11054 Wine trading in Gergovia (Gergovia 的酒交易)(贪心+模拟)

时间:2022-06-01 07:06:19

题意:直线上有n(2<=n<=100000)个等距的村庄,,每个村庄要么买酒,要么卖酒。设第i个村庄对酒的需求为ai(-1000<=ai<=1000),其中ai>0表示买酒,ai<0表示卖酒。所有村庄供需平衡,即所有ai之和等于0。把k个单位的酒从一个村庄运到相邻村庄需要k个单位的劳动力。计算最少需要多少劳动力可以满足所有村庄的需求。

分析:从最左面的村庄考虑,不管他是买酒还是卖酒,相对于他的相邻村庄都会有a0的运输量,所以运输量不断累加或抵消,一直算到最右边村庄即可。

#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 100000 + 10; const int MAXT = 10000 + 10; using namespace std; int main(){ int n; while(scanf("%d", &n) == 1){ if(!n) return 0; LL x, ans = 0, last = 0; for(int i = 0; i < n; ++i){ scanf("%lld", &x); ans += abs(last); last += x; } printf("%lld\n", ans); } return 0; }

  

UVA - 11054 Wine trading in Gergovia (Gergovia 的酒交易)(贪心+模拟)