zoukankan      html  css  js  c++  java
  • [HNOI2002]营业额统计

    找不到题目了,试敲了一下感觉还是挺不错的,学习了下别人的代码,差不多能感受到他的强大。哈哈。

    sl :可以说算是超级水提了,只要清楚这个数据结构就知道了。

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 using namespace std;
      5 const int MAX = 100000+10;
      6 int top;
      7 struct SBT {
      8     int L,R,size,key;
      9     void init() {
     10         L=R=0; size=1;
     11     }
     12 }T[MAX];
     13 
     14 void R_Roate(int &t) {   // 右旋转
     15     int k = T[t].L;
     16     T[t].L=T[k].R;
     17     T[k].R=t;
     18     T[k].size=T[t].size;
     19     T[t].size=T[T[t].R].size+T[T[t].L].size+1   ;
     20     t=k;
     21     return ;
     22 }
     23 void L_Roate(int &t) {  //左旋转
     24     int k=T[t].R;
     25     T[t].R=T[k].L;
     26     T[k].L=t;
     27     T[k].size=T[t].size;
     28     T[t].size=T[T[t].L].size+T[T[t].R].size+1;
     29     t=k;
     30     return ;
     31 }
     32 
     33 void Maintain(int &t,bool flag) {   //调整
     34     if(!flag) {
     35         if(T[T[T[t].L].L].size>T[T[t].R].size) {
     36             R_Roate(t);
     37         }
     38         else if(T[T[T[t].L].R].size>T[T[t].R].size) {
     39             R_Roate(T[t].L);
     40             R_Roate(t);
     41         }
     42         else return ;
     43     }
     44     else {
     45         if(T[T[T[t].R].R].size>T[T[t].L].size) {
     46             L_Roate(t);
     47         }
     48         else if(T[T[T[t].R].L].size>T[T[t].L].size) {
     49             R_Roate(T[t].R);
     50             L_Roate(t);
     51         }
     52         else return ;
     53     }
     54     Maintain(T[t].L,false);
     55     Maintain(T[t].R,true);
     56     Maintain(t,false);
     57     Maintain(t,true);
     58 }
     59 
     60 void Insert(int &t,int v) {   //插入
     61     if(t==0) {
     62         t=++top;
     63         T[t].init();
     64         T[t].key=v;
     65     }
     66     else {
     67         T[t].size++;
     68         if(T[t].key>v) Insert(T[t].L,v);
     69         else Insert(T[t].R,v);
     70         Maintain(t,v>=T[t].key);
     71     }
     72 }
     73 int Pred(int t,int v) {    //小于v的最大的数,没有小于v的数字则返回v.
     74     if(t==0) {
     75         return v;
     76     }
     77     if(v>T[t].key) {
     78         int res=Pred(T[t].R,v);
     79         if(res==v) return T[t].key;
     80         return res;
     81     }
     82     else return Pred(T[t].L,v);
     83 }
     84 int Succ(int t,int v) {    //返回大于v的最小的数字,全都小于v则返回v.
     85     if(t==0return v;
     86     if(v>=T[t].key) return Succ(T[t].R,v);
     87     else {
     88         int res=Succ(T[t].L,v);
     89         if(v==res) return T[t].key;
     90         return res;
     91     }
     92 }
     93 
     94 int Exist (int t,int v) {
     95     if(t==0return false;
     96     if(v<T[t].key) return Exist(T[t].L,v);
     97     else if(v==T[t].key) return true;
     98     else return Exist(T[t].R,v);
     99 }
    100 
    101 int main() {
    102     int n,ans=0,root=0,key; top=0;
    103     while(scanf("%d",&n)==1) {
    104         scanf("%d",&key); ans=0;
    105         ans+=key;
    106         Insert(root,key);
    107         for(int i=2;i<=n;i++) {
    108             scanf("%d",&key);
    109             if(Exist(root,key)) continue;
    110             int P=Pred(root,key),S=Succ(root,key);
    111             if(P==key)
    112             ans+=abs(key-S);
    113             else if(S==key)
    114                 ans+=abs(key-P);
    115             else
    116                 ans+=min(abs(key-P),abs(key-S));
    117             Insert(root,key);
    118         }
    119         printf("%d ",ans);
    120 
    121     }

    122 } 

  • 相关阅读:
    eclipse中文乱码
    学习进度(7)
    校园管家(Android开发团队项目)NABCD
    NABCD(团队项目)
    android studio连接真机大概问题
    01梦断代码阅读笔记
    学习进度(6)
    android studio学习(一)
    地铁出行系统1.0
    学习进度(4)
  • 原文地址:https://www.cnblogs.com/acvc/p/3918037.html
Copyright © 2011-2022 走看看