[等价转换] UVa11054 Wine trading in Gergovia Gergovia的酒交易

题目

题意:一题街道上很多酒店,交易葡萄酒,正数为卖出葡萄酒,负数为需要葡萄酒,总需求量和总售出量是相等的,从一家店到另外一家店需要路费(路费=距离×运算量),假设每家店线性排列且相邻两店之间距离都是1,求最小路费。

代码

#include <cstdio>
#include <cstdlib>
#include <cmath>

int main(){
    int n;
    while(scanf("%d",&n) == 1 && n){
        long long ans = 0, a, last = 0;
        for (int i = 0; i<n; i++){
            scanf("%Ild", &a);
            ans += abs(last);
            last += a;
        }
        printf("%Ild\n", ans);
    }

    return 0;
}

其他

1.LRJ所介绍的等价转换方法,并且他强调了,等价转换更多是一种思维方法,可以帮助选手理清思路,甚至可以直接得到问题的解决方案。
2.本体思路:
5 -4 1 -3 1
等价于 1 1 -3 1 (这种转换消耗了5劳动力)
等价于 2 -3 1 (-1)
等价于 -1 1 (-2)
完成 (-1)
5+1+2+1 = 9
也就是把五个村庄的交易,等价转换为4个村庄的交易,并且计算出等价转换的消耗(劳动力),最后转化为两个村庄交易的问题。

Wine Trading in Gergovia

12-27

DescriptionnnAs 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.nnThere 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.nnIn 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.nnInputnnThe input consists of several test cases.nnEach 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.nnThe last test case is followed by a line containing 0.nnOutputnnFor 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”).nnSample Inputnn5n5 -4 1 -3 1n6n-1000 -1000 -1000 1000 1000 1000n0nSample Outputnn9n9000

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭