poj 2940

news/2024/11/22 8:20:49/

 

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

5
5 -4 1 -3 1
6
-1000 -1000 -1000 1000 1000 1000
0

Sample Output

9
9000


思路:最终每个位置上的数应该是0;
    设now为当前剩余要搬的葡萄酒数目;ans为目前最小工作量。初始时now和ans为0;
    顺序扫描每个房间i(0<=i&&i<n):
      ans+=now;now+=a[i];
     最后得出的ans为最小工作量。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<iomanip>
 7 #include<cmath>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 using namespace std;
12 #define PI 3.141592653589792128462643383279502
13 #define N 100005
14 int main(){
15     //#ifdef CDZSC_June
16     freopen("in.txt","r",stdin);
17     //#endif
18     //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<n;i++){
23             scanf("%lld",a+i);
24             ans+=abs(now);
25             now+=a[i];
26         }
27     cout <<ans<<endl;
28     }
29 
30     return 0;
31 }
 

 

 

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


http://www.ppmy.cn/news/304065.html

相关文章

BZOJ2940 条纹

条纹游戏是一个双人的游戏。所需要的物品有一个棋盘以及三种颜色的长方形条纹&#xff0c;这三种颜色分别是红色、绿色和蓝色。所有的红色条纹的尺寸是c*1&#xff0c;所有的绿色条纹的尺寸是z*1&#xff0c;所有的蓝色条纹的尺寸是n*1&#xff0c;这里c,z,n是正整数。每种颜色…

Linux Crash/Hang on Bay Trail/J1900/N2940

近几年的linux kernel, 尤其是4.1以后,在Bay Trail平台上会随机挂起和死机,亲测j1900,死机非常频繁,而且死机前毫无征兆,直接就挂起了,console也没有相应。 这个问题在bugzilla.kernel.org上已经吵翻了,从2015年年初,一直到现在,仍然没有彻底解决,临时方案有几个,但…

docker下载慢,卡顿解决办法——免费安装人人都有的docker加速器

点我获取阿里云免费镜像加速器 先确认一下docker版本>1.10.0 docker -v 人人都有免费哦~ 进入对应目录查看&#xff0c;我是root用户且没有 daemon.json文件 那就创建一个 vim daemon.json内容就是网页复制的那个 最后重启 systemctl daemon-reloadsystemctl restart do…

国内vscode高速下载

将vscode官方下载地址中的az764295.vo.msecnd.net替换为vscode.cdn.azure.cn 下边这个是改好的可以直接使用&#xff0c;下载速度起飞 https://vscode.cdn.azure.cn/stable/dfd34e8260c270da74b5c2d86d61aee4b6d56977/VSCodeUserSetup-x64-1.66.2.exe

百度云盘高速下载

1.安装chrome浏览器 2.安装Free Download Manager 3.google应用商店搜索Free Download Manager&#xff0c;安装 4.google应用商店搜索Tampermonkey&#xff0c;安装 5.添加新脚本&#xff0c;搜索脚本 百度网盘直接下载助手 直链加速版 安装 6.登录网盘&#xff0c;选择要…

ubuntu高速下载onedrive文件

firefox&#xff08;chrome也可以&#xff0c;firefox&#xff09;打开onedirve网页版&#xff0c;按f12找到网络选项&#xff0c; 然后在onedirve网页版下载对应的文件&#xff0c; 在网络选项窗口里面找到download那个链接&#xff0c; 右键复制->复制为curl 命令链接&a…

chrome github加速器

下载加速插件 如果可以访问谷歌 github加速器下载 如果不能访问谷歌 csdn 资源文件下载 https://download.csdn.net/download/qq_26462567/16545761 使用方法 第一步: 打开chrome浏览器 第二步 &#xff1a;在地址栏输入 chrome://extensions 打开拓展程序 第三步&…

百度高速下载器

介绍 PanDownload&#xff0c;百度网盘高速下载器。采用了Aria 2技术&#xff0c;支持离线下载、新番下载、提取下载链接、自定义Aria2配置、自定义分享密码&#xff0c;原理类似油猴脚本IDM&#xff0c;获取直链后调用Aria2进行下载加速&#xff0c;登陆网盘账号获取自己的网…