UVa 11054:wine Trading in Gergovia

Source: Internet
Author: User

Link

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1995

"Original question"

As you could know from the comic "Asterix" and "Chieftain ' Shield", Gergovia consists to one street, and every inhabitant The city is a wine salesman. How do you wonder the economy works? Simple Enough:everyone buys wine from inhabitants to the city. Every Day, each inhabitant decides, how much wine he wants to the buy or sell. Interestingly, demand and supply are always the same, so that all 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 Gergovia don ' t care which persons they are doing with, trade they Only interested in selling or buying a specific amount of wine. They are clever enough to figure out a way of trading, so, overall amount of work needed for transports are minimize D.

In this problem your are asked to reconstruct the trading of one day in during. For simplicity we'll assume that's houses are built along a straight line with equal distance between adjacent. Transporting one bottle of wine from one house to a adjacent house results into 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's inhabitant living in the ith house wants to buy AI bottles of wine, otherwise if AI < 0, h E wants to sell-ai bottles of wine. You may assume this numbers AI sum up to 0. The last test case was followed by a line containing 0.

Output specification

For the all test case print the minimum amount of work units needed, so, every inhabitant has his demand. I/may assume so this number fits to a signed 64-bit integer (in C + + can use the data type "long Long", in JAVA The data type "long").

Sample Input

5
5-4 1-3 1
6
-1000-1000-1000 1000 1000 1000
0

Sample Output

9
9000

"The main effect of the topic"

A street living in continuous n households, not adjacent to two of people separated by a unit. Every household in the street needs to buy a certain amount of wine or sell the wine, ensuring that all people buy the same amount as the quantity sold. Each household can choose to trade with any other home. But because the distance is not the same, so need to travel expenses. The tolls are calculated by the distance of the transaction. Ask everyone to trade satisfaction, the minimum sum of the fare is how much.

"Analysis and Summary"

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

Because of the toll and distance, so you need to make the road as small as possible, so you can let each person and adjacent people to trade. Don't worry about whether they want to buy or sell. Suppose A1 to buy 5, A2 to sell 2, then let A1 to A2 buy 5, not tube A2 How many, not enough can play IOUs, become-3, then A3 will buy 3 A3. To do so, you need to get the smallest answer from the first enumeration to the last one.

Code

* 
 *  uva:11054-wine trading in Gergovia  
 *  result:accept 
 *  time:0.044s * author:d_ Double 
 */
#include <iostream>  
#include <cstdio>  
#include <cstring>  
# Include<cmath>  
using namespace std;  
      
int arr[100002];  
      
int main () {  
    int n;  
    while (scanf ("%d", &n) && N) {for  
        (int i=0; i<n; ++i)  
            scanf ("%d", &arr[i));  
        Long long ans=0;  
        for (int i=0; i<n-1; ++i) {  
            ans = ABS (arr[i]);  
            Arr[i+1] + = Arr[i];  
        }  
        printf ("%lld\n", ans);  
    }  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.