POJ 2940 Wine Trading in Gergovia [贪心]

news/2025/1/15 16:07:35/

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,数据有正负两种状态,(注意负数也是一种状态)我们可以从左往右看,将第一个数值多出来的部分往右移,不管第一个数是多少,一定会产生abs(a[0])个操作,然后把第一个状态值加到第二个状态,这一定是处理第一个状态的最小消耗,以此类推

代码很简单

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long  ll;
int main()
{ll n,m;while(scanf("%lld",&n) && n!=0){ll ans=0,a,tem=0;for(ll i=0;i<n;i++)scanf("%lld",&a),tem+=a,ans+=abs(tem);printf("%lld\n",ans);}
}

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

相关文章

HDU 2940 Hex Factorial

题目求N&#xff01;中有多少个0 用16进制高精度模拟 想起来一起以前做过的一道题&#xff0c;求末尾有多少个0&#xff0c;不能取0前边的3 4个非零位存下来进行计算&#xff0c;误差太大 #include <iostream> #include <fstream> #include <cstring> usin…

poj 2940 求和

#include <stdio.h>#include <stdlib.h>int main(){ int i,n,a,sum; sum0; scanf("%d%d",&a,&n); for(i0;i<n;i) { suma; aa%1010*a; } printf("%d\n",sum); return 0;} 转载于:https://www…

稳定的LDO芯片推荐

推荐几款稳定性较好的LDO芯片&#xff1a; LM2937&#xff1a;这是一款常用的低噪声LDO&#xff0c;具有良好的稳定性和高效率。 LM2675&#xff1a;这是一款高效率的LDO&#xff0c;特别适用于需要高输出精度的应用。 LM2937-N&#xff1a;这是LM2937的低噪声版本&#xff0c;…

poj2940

简单题 View Code #include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define maxn 100005int n, f[maxn];void input(){for (int i 0; i < n; i) scanf("%d", &f[i]);}void w…

百练 2940 求和

题目链接&#xff1a;http://bailian.openjudge.cn/practice/2940 # include <stdio.h>int main() {int Sn,a,a_back,n;int i;scanf("%d%d",&a,&n);a_backa; Sna;for(i1;i<n;i){aa*10a_back;SnSna;}printf("%d\n",Sn); return 0; } &am…

HDOJ2940高精度阶乘优化版

题目链接&#xff1a;hdoj2940-Hex Factorial 在此转到关于本书博客ACM大学生程序设计竞赛在线题库最新精选题解&#xff08;赵端阳&#xff09;部分解析 原书和网上大多数程序都是从0到200最高位不断加乘&#xff0c;然后判断前导零位置。实际上阶乘是递增记录的&#xff0c;可…

jzoj2940. 生成输入数据

Description 首先看到题目别太开心&#xff0c;这题可不是让你出数据&#xff5e;^_* 背景神马的就忽略了。这题就是给你一棵带边权的树&#xff0c;然后这棵树是某个完全图唯一的最小生成树。问原来的完全图中所有边可能的最小边权和是多少。 完全图是任意两个点之间都有边相连…

2940. 凤凰古城

单点时限: 2.0 sec 内存限制: 256 MB 凤凰古城&#xff0c;位于沱江之畔&#xff0c;群山环抱&#xff0c;关隘雄奇。碧绿的沱江依着城墙缓缓流淌&#xff0c;叠翠的南华山麓倒映江中。江中渔舟游船数点&#xff0c;山间暮鼓晨钟兼鸣&#xff0c;悬崖上的吊脚楼轻烟袅袅&…