首页 > 代码库 > (白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)

(白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)

题目地址:UVa 11054

很巧妙的一道题,这题是利用的等价转换,对每一条路来说,假如右边生产的比左边的多x,那么不管起点是哪,终点是哪,都可以把左右两侧的看成两个点,要从这条路上运送x个劳动力。再由于总和是0,所以只需要算出一端的总和就可以,这样只要遍历一遍就可以算出来了。写出代码就很简单了。。。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
int main()
{
    LL n, i, s, x, a;
    while(scanf("%lld",&n)!=EOF&&n)
    {
        s=0;
        x=0;
        for(i=0;i<n;i++)
        {
            scanf("%lld",&a);
            s+=abs(x);
            x+=a;
        }
        printf("%lld\n",s);
    }
    return 0;
}


(白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)