博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2940
阅读量:4495 次
发布时间:2019-06-08

本文共 2743 字,大约阅读时间需要 9 分钟。

 

Wine Trading in Gergovia
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3187   Accepted: 1454

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

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

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” or “__int64”, in JAVA the data type “long”).

Sample Input

55 -4 1 -3 16-1000 -1000 -1000 1000 1000 10000

Sample Output

99000 思路:最终每个位置上的数应该是0;     设now为当前剩余要搬的葡萄酒数目;ans为目前最小工作量。初始时now和ans为0;     顺序扫描每个房间i(0<=i&&i
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 using namespace std;12 #define PI 3.14159265358979212846264338327950213 #define N 10000514 int main(){15 //#ifdef CDZSC_June16 freopen("in.txt","r",stdin);17 //#endif18 //std::ios::sync_with_stdio(false);19 long long n,a[N],ans;int now;20 while(scanf("%lld",&n),n){21 ans=0;now=0;22 for(int i=0;i
 

 

 

转载于:https://www.cnblogs.com/yoyo-sincerely/p/5087245.html

你可能感兴趣的文章
px4的CMakelists.txt阅读
查看>>
linux-usb软件系统架构
查看>>
MySQL基础
查看>>
render Target sample in UI
查看>>
[转载]Linux用户管理全攻略(五)
查看>>
Django基础知识
查看>>
【python之路7】python基本数据类型(一)
查看>>
win7 实用
查看>>
Entity Framework Core 导航属性 加载数据
查看>>
String中的equals方法解析 jdk1.7
查看>>
Block详解
查看>>
Luogu P2467 [SDOI2010]地精部落 | 神奇的dp
查看>>
不可变类小结
查看>>
syslog-ng应用详解
查看>>
关于类加载器
查看>>
XCode: duplicate symbol 解决方案
查看>>
iOS状态栏设置详解!
查看>>
编程习惯1
查看>>
【读书笔记】iOS-给模拟器相册增加图片
查看>>
【Silverlight实例】Silverlight与Access数据库的互操作
查看>>