HYSBZ 1588 turnover statistics (splay)

Source: Internet
Author: User

Test instructions: Give a company the turnover of every day, the sum of the minimum fluctuation value of daily. Minimum fluctuation value for the day = min {Absolute value | The turnover of the day before-the turnover of the Day |}. The minimum fluctuation of the first day is its own.

Train of thought: splay stretching tree's entry question, only has the splay,insert,rotate these three main functions only.

Insert a number (turnover) into the tree and splay it to the root position, and then its predecessor and subsequent one closer to it to find the minimum fluctuation value. Note that there may be no precursors/successors. Special handling of the first number.

Note: The data in this question is defective, and the variable is zeroed before it is read into the turnover.

1#include <bits/stdc++.h>2 #definePII pair<int,int>3 #defineINF 0x7f7f7f7f4 #defineLL Long Long5 using namespacestd;6 Const intn=1000002;7 introot, node_cnt;8 structnode9 {Ten     intPre, Val, ch[2]; One }nod[n]; A  - intCreate_node (intVintFar//returns the node subscript - { theNod[node_cnt].val=v; -Nod[node_cnt].pre=Far ; -nod[node_cnt].ch[0]=0; -nod[node_cnt].ch[1]=0; +     returnnode_cnt++; - } +  A voidRotate (intTintD//d is direction, 0 is left, 1 is right at { -     intFar=Nod[t].pre; -     intSON=NOD[T].CH[D];//Far 's Child -     intGra=nod[far].pre;//Far 's father -  -Nod[son].pre=Far ; inNod[t].pre=GRA; -Nod[far].pre=T; to  +nod[far].ch[d^1]=Son; -nod[t].ch[d]=Far ; thenod[gra].ch[nod[gra].ch[1]==far]=T; * } $ Panax Notoginseng voidSplay (intTintGoal//any child who converts t to goal - { the      while(Nod[t].pre!=goal)//T is not yet the root +     { A         intF=nod[t].pre, g=Nod[f].pre; the         if(G==goal) Rotate (T, nod[f].ch[0]==T);//father is the root s, spin 1 times +         Else -         { $             intD1= (nod[f].ch[0]==t), d2= (nod[g].ch[0]==f); $             if(D1==D2)//two times of the same rotation -             { - Rotate (f, D1); the Rotate (t, D1); -             }Wuyi             Else            //Two reverse rotation the             { - Rotate (t, D1); Wu Rotate (T, D2); -             } About         } $     } -     if(!goal) root=t;//Always update roots - } -  A intInsert (intTintv) + { the     if(V==nod[t].val)return-1; -     intq=-1; $     if(V>nod[t].val)//Right the     { the         if(nod[t].ch[1]==0) q= (nod[t].ch[1]=Create_node (V, t)); the         ElseQ=insert (nod[t].ch[1], v); the     } -     Else                //left in     { the         if(nod[t].ch[0]==0) q= (nod[t].ch[0]=Create_node (V, t)); the         ElseQ=insert (nod[t].ch[0], v); About     } the     returnQ; the } the intGet_pre (intTintD//seeking precursors and successors, d=1 representatives seeking precursors + { -     if(Nod[t].ch[d])returnGet_pre (Nod[t].ch[d], d); the     returnNod[t].val;Bayi } the  the voidInit () - { -Root=node_cnt=0; theCreate_node (INF,0);//No. No. 0 point is not. the } the intMain () the { -Freopen ("Input.txt","R", stdin); the     intN; the      while(cin>>N) the     {94 init (); the         intans=0; the          for(intI=0, a=0; i<n; I++,a=0) the         {98scanf"%d", &a); About             intt=Insert (Root, a); -             if(t<0)Continue;//This number already exists101Splay (T,0);//extend T to the root102             if(i==0) Ans=a;//First Number103             Else if(nod[t].ch[0] && nod[t].ch[1] )104Ans+=min (ABS (A-get_pre (nod[t].ch[0],1), ABS (A-get_pre (nod[t].ch[1],0))); the             Else if(nod[t].ch[0]) Ans+=abs (A-get_pre (nod[t].ch[0],1) );106             Else if(nod[t].ch[1]) Ans+=abs (A-get_pre (nod[t].ch[1],0) );107 108         }109cout<<ans<<Endl; the     }111     return 0; the}
AC Code

HYSBZ 1588 turnover statistics (splay)

Related Article

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.