"BZOJ-1588" turnover statistics splay

Source: Internet
Author: User

1588: [HNOI2002] turnover statistics time limit:5 Sec Memory limit:162 MB
submit:12485 solved:4508
[Submit] [Status] [Discuss] 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

SourceSolution

Splay template questions, read an X, insert X, and then look at the predecessor of X, classify and discuss ....

Code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 100010intN;intSz,root;intfa[maxn],son[maxn][2],KEY[MAXN],CNT[MAXN],SIZE[MAXN];voidClearintNow ) {son[now][1]=son[now][0]=fa[now]=key[now]=size[now]=cnt[now]=0;}int Get(intNow ) {    returnson[fa[now]][1]==Now ;}voidUpdateintNow ) {    if(!now)return; Size[now]=Cnt[now]; if(son[now][0]) size[now]+=size[son[now][0]]; if(son[now][1]) size[now]+=size[son[now][1]];}voidRotateintNow ) {    intOld=fa[now],oldf=fa[old],which=Get(now); Son[old][which]=son[now][which^1]; fa[son[old][which]]=Old ; Fa[old]=now; son[now][which^1]=old; fa[now]=Oldf; if(OLDF) son[oldf][son[oldf][1]==old]=Now ; Update (old); Update (now);}voidSplay (intNow ) {     for(intF (f=Fa[now]); Rotate (now))if(Fa[f])if(Get(now) = =Get(f) Rotate (f); Elserotate (now); Root=Now ;}voidInsertintv) {    if(!root) {SZ++;son[sz][0]=son[sz][1]=fa[sz]=0; key[sz]=v;size[sz]=1; cnt[sz]=1; Root=sz;return;} intnow=root,f=0;  while(true)        {            if(key[now]==v) {Cnt[now]++;update (now), update (f); splay (now); Break;} F=now; now=son[now][key[now]<v]; if(now==0) {sz++;son[sz][0]=son[sz][1]=0; key[sz]=v;size[sz]=1; CNT[SZ]=1; fa[sz]=f;son[f][key[f]<v]=sz; Update (f); splay (SZ); Break; }        }}intprev () {intnow=son[root][0];  while(son[now][1]) now=son[now][1]; returnNow ;}intsucc () {intnow=son[root][1];  while(son[now][0]) now=son[now][0]; returnNow ;} intMain () {n=read ();intans=0;  for(intI=1; i<=n; i++)        {            intx=read (); insert (x); intPre=key[prev ()],suc=KEY[SUCC ()]; if(i==1) {ans=x;Continue;} if(cnt[root]>1)Continue; if(prev () &&succ ())if(ABS (Pre-x) <abs (suc-x)) Ans+=abs (pre-x); ElseAns+=abs (suc-x); Else if(!prev ()) Ans+=abs (suc-x); Else if(!SUCC ()) Ans+=abs (pre-x); } printf ("%d\n", ans); return 0;}

At first, I didn't see any problems with Bzoj. Ghost Animal not only. And do this water problem, a start 9a1w, and on the 1, feeling very wonderful, found that hand mistakenly hit a variable, MD This can be over nine not error ...

"BZOJ-1588" turnover statistics splay

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.