POJ - 2940

news/2024/11/25 5:12:08/

题目
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
思路:从第一个位置开始,带着数字往右走,每到一个位置,和当前位置元素相加减

#include <stdio.h>
#include <math.h>
int main()
{int n,a[100010];while(scanf("%d",&n)!=EOF&&n){long long sum=0,len=0;for(int i=0;i<n;i++){scanf("%d",&a[i]);sum+=a[i];len+=fabs(sum);}printf("%lld\n",len);}return 0;
} 

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

相关文章

poj 2940

Wine Trading in Gergovia Time Limit: 1000MS Memory Limit: 65536KTotal 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 …

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 打开拓展程序 第三步&…