【高效算法设计——等价转换】UVa 11054
2015-03-09 12:16
295 查看
Wine trading in Gergovia
Submit Status
Description
2006/2007 ACM International Collegiate Programming Contest
University of Ulm Local Contest
buys wine from other inhabitants of the city. Every day each inhabitant decides how much wine he wants to buy or sell. Interestingly, demand and supply is always the same, so that each inhabitant gets what he wants.
There is one problem, however: Transporting wine from one house to another results in work. Since all wines are equally good, the inhabitants of Gergovia don't care which persons they are doing trade with, they
are only interested in selling or buying a specific amount of wine. They are clever enough to figure out a way of trading so that the overall amount of work needed for transports is minimized.
In this problem you are asked to reconstruct the trading during one day in Gergovia. For simplicity we will assume that the houses are built along a straight line with equal distance between adjacent houses.
Transporting one bottle of wine from one house to an adjacent house results in one unit of work.
1000). If ai ≥ 0, it means that the inhabitant living in the ith house wants to buy ai bottles of wine, otherwise if ai < 0, he wants to sell -ai bottles of wine. You may assume that the numbers ai sum
up to 0.
The last test case is followed by a line containing 0.
Source
题意:有n个位置等距的村庄,输入它们各自对酒的需求或剩余量,求最小搬运的代价使得每个酒庄的酒需求量都为0
思路:无论是需要还是有余,需要搬运的代价是相同的,我们可以维护一个变量ret,表示前i个村庄互相搬运后,还需要/剩余多少酒,即ret=sum[i],每个i的ret的绝对值就表示到第i个村庄所花费的最小代价
注意本题数据量很大,如果n取上限,并且给出的数据为左边一半全部是1000,右边一半全部是-1000,那么ans必然超int,所以应使用long long
代码如下
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
2006/2007 ACM International Collegiate Programming Contest
University of Ulm Local Contest
Wine trading in Gergovia
As you may know from the comic "Asterix and the Chieftain's Shield", Gergovia consists of one street, and every inhabitant of the city is a wine salesman. You wonder how this economy works? Simple enough: everyonebuys wine from other inhabitants of the city. Every day each inhabitant decides how much wine he wants to buy or sell. Interestingly, demand and supply is always the same, so that each inhabitant gets what he wants.
There is one problem, however: Transporting wine from one house to another results in work. Since all wines are equally good, the inhabitants of Gergovia don't care which persons they are doing trade with, they
are only interested in selling or buying a specific amount of wine. They are clever enough to figure out a way of trading so that the overall amount of work needed for transports is minimized.
In this problem you are asked to reconstruct the trading during one day in Gergovia. For simplicity we will assume that the houses are built along a straight line with equal distance between adjacent houses.
Transporting one bottle of wine from one house to an adjacent house results in one unit of work.
Input Specification
The input consists of several test cases. Each test case starts with the number of inhabitants n (2 ≤ n ≤ 100000). The following line contains n integers ai (-1000 ≤ ai ≤1000). If ai ≥ 0, it means that the inhabitant living in the ith house wants to buy ai bottles of wine, otherwise if ai < 0, he wants to sell -ai bottles of wine. You may assume that the numbers ai sum
up to 0.
The last test case is followed by a line containing 0.
Output Specification
For each test case print the minimum amount of work units needed so that every inhabitant has his demand fulfilled. You may assume that this number fits into a signed 64-bit integer (in C/C++ you can use the data type "long long", in J***A the data type "long").Sample Input
5 5 -4 1 -3 1 6 -1000 -1000 -1000 1000 1000 1000 0
Sample Output
9 9000
Source
题意:有n个位置等距的村庄,输入它们各自对酒的需求或剩余量,求最小搬运的代价使得每个酒庄的酒需求量都为0
思路:无论是需要还是有余,需要搬运的代价是相同的,我们可以维护一个变量ret,表示前i个村庄互相搬运后,还需要/剩余多少酒,即ret=sum[i],每个i的ret的绝对值就表示到第i个村庄所花费的最小代价
注意本题数据量很大,如果n取上限,并且给出的数据为左边一半全部是1000,右边一半全部是-1000,那么ans必然超int,所以应使用long long
代码如下
#include<cstdio> #include<cstring> #include<cmath> using namespace std; const int maxn=100000+5; int A[maxn]; int main() { int i,j,n; long long ans,ret; while(~scanf("%d",&n) && n) { for(i=1;i<=n;i++) { scanf("%d",&A[i]); } ans=ret=0; for(i=1;i<=n;i++) { ans+=abs(ret); ret+=A[i]; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 高效算法设计专项:UVa 11054
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers
- 【高效算法设计——滑动窗口】UVa 11572 Unique Snowflakes
- 【高效算法设计——递归】 UVa 1608 Non-boring sequences
- 【高效算法设计——滑动窗口】 UVa 12174 Shuffle
- Uva11054 Wine trading in Gergovia【例题8-5】【等价转换】
- ( 设计高效算法 ) 年龄排序 Age Sort Uva 11462
- 高效算法设计专项:UVa 10827
- 高效算法设计专项:UVa 11572
- (高效算法设计)之高维问题 废料堆 Garbage heap Uva 10755
- 【高效算法设计——跳跃枚举】Uva 11093 Just Finish it up
- 高效算法设计专项:UVa 10125
- [等价转换]UVa11054 - Wine trading in Gergovia
- 高效算法设计专项:UVa 10535
- 【高效算法设计】UVa120 Stack of Flapjacks
- 【高效算法设计——最大值最小问题】UVa 714 Coying Books
- 高效算法设计专项:UVa 10810
- 高效算法设计专项:UVa 10691
- (白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)
- 【高效算法设计——递归】 UVa 12627 Erratic Expansion