首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

poj2940 Wine Trading in Gergovia-见地题

2012-09-15 
poj2940Wine Trading in Gergovia-----想法题#include iostream#include cstdlib#include cstring#i

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;}

热点排行