1588: [HNOI2002] turnover statistics time limit:5 Sec Memory limit:162 MB
submit:9203 solved:3097
[Submit] [Status] Description
Turnover statistics Tiger has recently been promoted to Sales manager, the first task he received after taking office was to count and analyze the company's business since its inception. Tiger took out the company's ledger, which recorded the daily turnover of the company since its inception. Analysis of the business situation is a rather complicated task. Due to holidays, big sale or other circumstances, the turnover will be a certain fluctuation, of course, certain fluctuations are acceptable, but at some point the turnover is very high or low, which proves that the company at this time the operation of the situation has been a problem. The economic management defines a minimum fluctuation value to measure the situation: the greater the minimum fluctuation of the day, the greater the worth, the more unstable the business situation. And the analysis of the entire company from the establishment to the current business is stable, only need to put the minimum fluctuation of each day to add up on it. Your task is to write a program to help Tiger calculate this value. The first day of the minimum fluctuation is the first day of turnover.? input and output requirements
Input
The first act is a positive integer representing the number of days from the time the company was established to the present, and the next n rows have an integer (possibly negative) on each line, representing the turnover of the company for the day I.
Output
The output file has only a positive integer, which is sigma (the minimum daily fluctuation value). The result is less than 2^31.
Sample Input6
5
1
2
5
4
6Sample Output12
HINT
Result Description: 5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
This problem data, see the discussion version http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=1588
Source
The problem: Well, a very classic balance tree--oh, I generally like to use a simple and well-written treap--as long as the insertion of each point in the process of recording the absolute value of each point and the insertion point, and then all the way daleitai to the minimum can be, so the child gently loose O (Nlogn) Water over (hansbug: My 220ms,phile god Ben 252ms good happy? * ^_^ *)
1 var2 I,j,k,l,m,n,mt,head:longint;3A,lef,rig,fix:Array[0..1000000] ofLongint;4 functionmin (x,y:longint): Longint;inline;5 begin6 ifX<y ThenMin:=xElsemin:=y;7 End;8 procedureRtvarx:longint); inline;9 varF,l:longint;Ten begin One if(x=0)or(lef[x]=0) Thenexit; Af:=x;l:=Lef[x]; -lef[x]:=Rig[l]; -rig[l]:=x; thex:=L - End; - procedureLtvarx:longint); inline; - varF,r:longint; + begin - if(x=0)or(rig[x]=0) Thenexit; +f:=x;r:=Rig[x]; Arig[x]:=Lef[r]; atlef[r]:=x; -x:=R; - End; - procedureInsvarx,y:longint); inline; - begin - ifx=0 Thenexit; in ifA[Y]<A[X] Then - begin toMt:=min (Mt,abs (a[x]-a[y])); + iflef[x]=0 ThenLef[x]:=yElseins (lef[x],y); - ifFIX[LEF[X]]>FIX[X] ThenRT (x); the End * Else $ beginPanax NotoginsengMt:=min (Mt,abs (a[x]-a[y])); - ifrig[x]=0 ThenRig[x]:=yElseins (rig[x],y); the ifFIX[RIG[X]]>FIX[X] ThenLT (x); + End; A End; the begin + READLN (n); - Randomize; $Fillchar (Lef,sizeof (LEF),0); $Fillchar (rig,sizeof rig),0); - fori:=1 toN Do - begin the read (a[i]); -fix[i]:=random (maxlongint);Wuyi End; thehead:=1; -l:=a[1]; Wu fori:=2 toN Do - begin Aboutmt:=maxlongint;j:=i; $ ins (head,j); -l:=l+MT; - End; - Writeln (l); A End. +
1588: [HNOI2002] turnover statistics