解题报告 之 UVA11054 Wine trading in Gergovia
来源:互联网 发布:江苏网络协会 编辑:程序博客网 时间:2024/06/05 03:16
解题报告 之 UVA11054 Wine trading in Gergovia
Description
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: everyone 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.
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 -aibottles 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 JAVA the data type "long").Sample Input
55 -4 1 -3 16-1000 -1000 -1000 1000 1000 10000
Sample Output
99000
#include<iostream>using namespace std;int main(){int n;while (cin >> n&&n){long long num,last=0,ans=0;for (int i = 0; i < n; i++){cin >> num;last += num;ans += abs(last);}cout << ans<<endl;}}
今天都是水题效率好高的样子~~哈哈哈哈
- 解题报告 之 UVA11054 Wine trading in Gergovia
- uva11054 - Wine trading in Gergovia
- UVA11054 Wine trading in Gergovia
- uva11054 - Wine trading in Gergovia
- UVa11054 Wine trading in Gergovia
- [贪心]UVA11054 Wine trading in Gergovia
- UVa11054 - Wine trading in Gergovia UVA
- [问题分解]UVa11054 - Wine trading in Gergovia
- [等价转换]UVa11054 - Wine trading in Gergovia
- UVa11054--Wine trading in Gergovia(贪心)
- Uva11054——Wine trading in Gergovia
- Gergovia的酒交易(Uva11054,Wine trading in Gergovia)
- UVA11054:Wine trading in Gergovia(Gergovia的酒交易)
- 【Jason's_ACM_解题报告】Wine trading in Gergovia
- uva11054 - Wine trading in Gergovia(葡萄酒交易)
- UVa11054 poj2940 sdut2370 Wine trading in Gergovia(贪心)
- Uva11054 Wine trading in Gergovia【例题8-5】【等价转换】
- UVa11054 Wine trading in Gergovia(贪心+思路)
- Eclipse+PyDev 安装和配置
- java自学day02
- 开发者应该了解的 12 款 Eclipse 插件
- CMD常用命令
- 07 shell数组
- 解题报告 之 UVA11054 Wine trading in Gergovia
- 关于android学习的最近总结-2
- 在SQL Server中,当你处理Unicode字符串常量的时候,你必须在所有Unicode字符串前添加N前缀
- 精通shell编程第二版Sriranga Veeraraghavan著 卢涛notes
- 交大住宿费缴费回执复印件6月6号之前交到中财大厦1001
- Robot Framework中文站
- Linux 不回显输入代码
- 《像程序员一样思考》
- Android Studio中NDK开发