UVa 11054 Wine trading in Gergovia(掃描),11054gergovia

來源:互聯網
上載者:User

UVa 11054 Wine trading in Gergovia(掃描),11054gergovia

題意  有n個村莊  第i個村莊需要買a[i]的酒   a[i]為負時該村莊可賣掉-a[i]的酒  保證所有a[i]的和為0   一個單位的酒從一個村莊運輸到相鄰村莊的消耗為1  求運輸完所有酒需要的最小消耗

總消耗最少時  每個需要買的村莊都會找離他最近的可以賣的村莊  容易發現  這種狀況下  從第一個村和第二個村莊之間的運輸量為abs(a[1])  第二個村莊和第三個村莊之間的運輸量為abs(a[1]+a[2])   第k個村莊到第k+1個村莊的運輸量為abs(a[1]+a[2]+...+a[k])

#include <bits/stdc++.h>using namespace std;int main(){    int n, t;    long long ans, last;    while(~scanf("%d", &n), n)    {        ans = last = 0;        //last記錄需要從i-1運輸到i的量        for(int i = 1; i <= n; ++i)        {            scanf("%d", &t);            ans += abs(last), last += t;        }        printf("%lld\n", ans);    }    return 0;}

Wine trading in Gergovia

As 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.

There 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.

In 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.

Input Specification

The input consists of several test cases. Each 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.
The last test case is followed by a line containing 0.

Output SpecificationFor 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", in JAVA the data type "long").Sample Input
55 -4 1 -3 16-1000 -1000 -1000 1000 1000 10000
Sample Output
99000


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.