[HNOI2002] Turnover statistics time limit:5 Sec Memory limit:162 MB
submit:4128 solved:1305
Description turnover Statistics Tiger was recently 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 minimum fluctuation for the first day is the turnover of the first day. Input and output requirements input first positive integer, indicating that the company from the establishment of the number of days to the present day, the next n rows each row has a positive integer representing the company's turnover. 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
Splay
Look at the faint, let half copy of the code to write down, later slowly brush the proficiency of the good
1 /**/2#include <iostream>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <algorithm>7 using namespacestd;8 intinf=100000000;9 Const intmxn=120000;Ten intv[mxn],ch[mxn][2],fa[mxn],root=0, cnt=0; One intt1,t2; A intN; - voidRotateintXint&k) { - inty=fa[x],z=Fa[y]; the intL,r; - if(ch[y][0]==X) l=0;ElseL=1; -r=l^1; - if(y==k) k=x; + Else{ - if(ch[z][0]==y) ch[z][0]=x; + Elsech[z][1]=x; A } atfa[x]=z;fa[y]=x;fa[ch[x][r]]=y; -ch[y][l]=Ch[x][r]; -ch[x][r]=y; - } - voidSplay (intXint&k) { - inty,z; in while(x!=k) { -y=fa[x];z=Fa[y]; to if(y!=k) { + if((ch[y][0]==x) ^ (ch[z][0]==y)) Rotate (x,k); - Elserotate (y,k); the } * rotate (x,k); $ }Panax Notoginseng return; - } the voidInsertint&k,intValintfather) { + if(!k) { Ai=i+CNT; thev[k]=Val; +fa[k]=father; - splay (k,root); $ return; $ } - if(Val<v[k]) insert (ch[k][0],val,k); - Else if(Val>v[k]) insert (ch[k][1],val,k); the } - voidAsk_1 (intKintx) {//Seeking precursorsWuyi if(!k)return; the if(x>=V[k]) { -t1=V[k]; WuAsk_1 (ch[k][1],x); -}ElseAsk_1 (ch[k][0],x); About return; $ } - voidAsk_2 (intKintx) {//Seeking precursors - if(!k)return; - if(x<=V[k]) { AT2=V[k]; +Ask_2 (ch[k][0],x); the}ElseAsk_2 (ch[k][1],x); - return; $ } the intMain () { thescanf"%d",&n); the inti,j,x; the BOOLfirst=1; - intans=0; in for(i=1; i<=n;i++){ thescanf"%d",&x); thet1=-INF; AboutT2=INF; the if(first) { thefirst=0; theans+=x; + } - Else { the Ask_1 (root,x);Bayi ask_2 (root,x); theAns+=min (x-t1,t2-x); the } -Insert (Root,x,0); - } theprintf"%d\n", ans); the return 0; the}
BZOJ1588 HNOI2002 turnover statistics [splay introduction]