读书人

poj2940 Wine Trading in Gergovia-见

发布时间: 2012-09-15 19:09:29 作者: rapoo

poj2940 Wine Trading in Gergovia-----想法题
#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 work(){ int i = 0, j = 0; long long ans = 0; while (1) { while (i < n && f[i] <= 0) i++; while (j < n && f[j] >= 0) j++; if (j >= n || i >= n) break; int x = min(f[i], -f[j]); f[i] -= x; f[j] += x; ans += x * abs(i - j); } printf("%lld\n", ans);}int main(){ while (scanf("%d", &n), n) { input(); work(); } return 0;}

读书人网 >编程

热点推荐