As know from the comic \asterix and the chieftain ' s Shield ", Gergovia consists of one street,
And every inhabitant of the city are a wine salesman. Wonder how is this economy works? Simple
Enough:everyone buys wine from and inhabitants of the city. Every day all inhabitant decides how
Much wine he wants to buy or sell. Interestingly, demand and supply are always the same
Inhabitant gets what he wants.
There is one problem, however:transporting wine from one house to another results in work. Since
All wines is equally good, the inhabitants of Gergovia don ' t care which persons they is doing trade
With, they is only interested in selling or buying a specic amount of wine. They is clever enough
To gure out a-trading so, the overall amount of work needed for transports are minimized.
In this problem you is asked to reconstruct the trading during one day in Gergovia. For simplicity
We'll assume that the houses is built along a straight line with equal distance between adjacent
Houses. Transporting one bottle of wine from a house to a adjacent house results in one unit of
Work.
Input
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 (?? + AI 1000). If AI 0, it
means the inhabitant living in the I-th House wants to buy AI bottles of wine, otherwise if AI < 0,
He wants to sell?? AI bottles of wine. Assume that the numbers AI sum up to 0.
The last test case was followed by a line containing ' 0 '.
Output
For all test case print the minimum amount of work units needed so that every inhabitant have his
Demand fullled. Assume that this number TS into a signed 64-bit integer
Can use the data type \long long "in JAVA the data type \long").
1#include <cstdio>2#include <cstring>3 Long Longa[100010];4 Long LongAbsLong Longx)5 {6 returnx>=0? x:-x;7 }8 intMain ()9 {Ten inti,j,k,m,n,p,q; One Long LongX,y,z,ans; A while(SCANF ("%d", &n) &&N) - { - for(i=1; i<=n;i++) thescanf"%lld",&a[i]); -ans=0; - for(i=1; i<n;i++) - { +ans+=ABS (A[i]); -a[i+1]+=A[i]; + } Aprintf"%lld\n", ans); at } -}
With the induction of "I have seen before a word, do not know exactly inappropriate" method analysis is as follows.
1. To make the 1th point meet the requirements, it must come from point 2nd (whether it is the 2nd point itself or from a later point), so the demand for the 1th point is met and transferred to Point 2nd.
2. If the n-1 point and the left meet the requirements, then for the N point, his needs must be met by the n+1 point (whether the n+1 itself or later). So the n points are met and transferred to the n+1 point.
Since the above two points are established, we should scan from left to right and transfer the demand to the next point.
UVA 11054 Wine Trading in Gergovia (inductive "OK this is my own name")--yhx