Test instructions
There are n equidistant wineries in a straight line, each winery's demand for wine is AI (a positive number indicates the need to buy wine, a negative number is required to sell alcohol), and the balance of supply and demand is ensured for all wineries.
Transporting X-units of wine to neighboring wineries requires X labor, which requires a minimum of labor to balance the supply and demand of all wineries.
Analysis:
The first winery buys A1 units of wine, which may be produced directly from a second winery or another winery to a second winery.
But in the end there must be A1 a labor force from the winery 2 to the winery 1.
As a result, the problem is translated into:
There are 2~n these wineries, the winery 2 demand for A1+A2, the demand for other wineries or AI. and has used the |a1| labor force, the need for a minimum of labor.
In fact, both positive and negative numbers correspond to this conversion.
So, we can read the side count on it.
Because of the large amount of data, so I Machine (Wei) wisdom (suo) to use an input hanging, sure enough a lot faster.
1#include <cstdio>2#include <cmath>3 4 Long LongScan () {//input Plug -in5 intres =0, flag =0;6 Charch;7 if(ch = getchar ()) = ='-') flag =1;8 Else if(Ch >='0'&& CH <='9') res = CH-'0';9 while(ch = getchar ()) >='0'&& CH <='9')Tenres = res *Ten+ (CH-'0'); One returnFlag? -Res:res; A } - - intMain () the { - //freopen ("In.txt", "R", stdin); - - intN; + while(SCANF ("%d", &n) = =1&&N) - { + GetChar (); A Long LongAns =0, last =0; at intA; - for(inti =0; I < n; ++i) - { -A =Scan (); -Ans + =Std::abs (last); -Last + =A; in } - toprintf"%lld\n", ans); + } - the return 0; *}
code June
UVa 11054 Wine Trading in Gergovia