zoukankan      html  css  js  c++  java
  • HNOI2004宠物收养所

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1208

    1208: [HNOI2004]宠物收养所

    Time Limit: 10 Sec  Memory Limit: 162 MB

    Description

    最 近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物,阿Q根据领养 者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物 一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太 少。 1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。 (任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点 值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。 2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养 者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。 一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。【任务描述】你得到了一年 当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

    Input

    第 一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养 所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养 者,这些宠物和领养者的个数不会超过10000个)

    Output

    仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

    Sample Input

    5
    0 2
    0 4
    1 3
    1 2
    1 5

    Sample Output

    3
    (abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)

    HINT

    Source

    做法:裸Splay或者直接用Set(红黑树啧啧啧。。用lower_bound-1和upper_bound检测下即可)
    Codes:
      1 #include <set>
      2 #include <queue>
      3 #include <cstdio>
      4 #include <cstdlib>
      5 #include <cstring>
      6 #include <iostream>
      7 #include <algorithm>
      8 using namespace std;
      9 const int Mod = 1000000;
     10 #define For(i,n) for(int i=1;i<=n;i++)
     11 #define Rep(i,l,r) for(int i=l;i<=r;i++)
     12 set<int>::iterator Min,Max;
     13 set<int> T;
     14 int n,nowa,nowb,ans;
     15 
     16 int main(){
     17     scanf("%d",&n);
     18     For(i,n){int op,Val;
     19         scanf("%d %d",&op,&Val);
     20         if(op){
     21             if(nowa){
     22                 nowa--;
     23                 Min = T.lower_bound(Val);
     24                 if((*Min!=Val)&&(Min!=T.begin())) 
     25                     Min--;
     26                 Max = T.upper_bound(Val);
     27                 if(abs(*Min-Val)<=abs(*Max-Val)){
     28                     ans = (ans + abs(*Min-Val)) % Mod;
     29                     T.erase(Min);
     30                 }
     31                 else{
     32                     ans = (ans + abs(*Max-Val)) % Mod;
     33                     T.erase(Max);
     34                 }
     35             }
     36             else{
     37                 T.insert(Val);
     38                 nowb++;    
     39             }
     40         }else{
     41             if(nowb){
     42                 nowb--;          
     43                 Min = T.lower_bound(Val);
     44                 if((*Min!=Val)&&(Min!=T.begin())) Min--;
     45                 Max = T.upper_bound(Val);
     46                 if(abs(*Min-Val)<=abs(*Max-Val)){
     47                     ans = (ans + abs(*Min-Val)) % Mod;
     48                     T.erase(Min);
     49                 }
     50                 else{
     51                     ans = (ans + abs(*Max-Val)) % Mod;
     52                     T.erase(Max);
     53                 }
     54             }else{
     55                 nowa++;
     56                 T.insert(Val);
     57             }
     58         }
     59     }
     60     printf("%d
    ",ans);
     61     return 0;
     62 }
     63 ----------------------下面是我的渣渣splay..-------------------- 
     64 #include <set>
     65 #include <cmath>
     66 #include <queue>
     67 #include <vector>
     68 #include <cstdio>
     69 #include <cstdlib>
     70 #include <cstring>
     71 #include <iostream>
     72 #include <algorithm>
     73 using namespace std;
     74 const int N = 80010;
     75 const int Mod = 1000000;
     76 const int maxint = 2147483647;
     77 #define fa(i) (T[i].p)
     78 #define Loc(i) (T[fa(i)].s[1]==i)
     79 #define For(i,n) for(int i=1;i<=n;i++)
     80 #define Rep(i,l,r) for(int i=l;i<=r;i++)
     81 #define Sets(a,b,Loc) {T[a].s[Loc] = b;fa(b) = a;}
     82  
     83 struct tnode{
     84     int s[2],v,p;
     85 }T[N];
     86  
     87 int n,root,nowa,nowb,tot;
     88 int ans;
     89  
     90 void Rot(int i){
     91     int x = i , y = fa(x) , z = fa(y);
     92     int lx = Loc(x) , ly = Loc(y) , lz = Loc(z);
     93     Sets(y,T[x].s[!lx],lx);
     94     Sets(z,x,ly);
     95     Sets(x,y,!lx);
     96 }
     97  
     98 void Splay(int i,int goal){
     99     for(;fa(i)!=goal;){
    100         if(fa(fa(i))!=goal) Rot(fa(i));
    101         Rot(i);
    102     }
    103     if(!goal) root = i;
    104 }
    105  
    106 int Pred(int i){
    107     int Next = T[i].s[0];
    108     if(!Next) return Next;
    109     while(T[Next].s[1]) Next = T[Next].s[1];
    110     return Next;
    111 }
    112  
    113 int Succ(int i){
    114     int Next = T[i].s[1]; 
    115     if(!Next) return Next;
    116     while(T[Next].s[0]) Next = T[Next].s[0];
    117     return Next;
    118 }
    119  
    120 void Delete(int i){
    121     Splay(i,0);
    122     if(T[i].s[0]){
    123         Splay(Pred(i),i);
    124         Sets(T[i].s[0],T[i].s[1],1);
    125         fa(T[i].s[0]) = 0;
    126         root = T[i].s[0];
    127     }else{
    128         if(T[i].s[1]){root = T[i].s[1];fa(T[i].s[1]) = 0;}
    129         else          root = 0;
    130     }
    131     T[i].s[0] = T[i].s[1] = T[i].v = T[i].p = 0;
    132 }
    133  
    134 int Find(int V,int i){
    135     if(V==T[i].v){
    136         Delete(i);
    137         return V;
    138     }else
    139     if(V>T[i].v){
    140         Splay(i,0);
    141         int succ = Succ(i);
    142         if((V>T[succ].v)&&(succ)) return Find(V,succ);
    143         else{
    144             if(abs(V-T[i].v)<=abs(V-T[succ].v)){
    145                 int temp = T[i].v;
    146                 Delete(i);
    147                 return temp;
    148             }else{
    149                 int temp = T[succ].v;
    150                 Delete(succ);
    151                 return temp;
    152             }
    153         }
    154     }else{
    155         Splay(i,0);
    156         int pred = Pred(i);
    157         if((V<T[pred].v)&&(pred)) return Find(V,pred);
    158         else{
    159             if(abs(V-T[pred].v)<=abs(V-T[i].v)){
    160                 int temp = T[pred].v;                
    161                 Delete(pred);
    162                 return temp;
    163             }else{
    164                 int temp = T[i].v;
    165                 Delete(i);
    166                 return temp;
    167             }
    168         }
    169     } 
    170 }
    171  
    172 void Insert(int V,int i){
    173     if(!i){
    174         root = ++tot;T[root].v = V;T[root].p = 0;
    175         return;
    176     }
    177     if(!T[i].s[V>T[i].v]){
    178         T[i].s[V>T[i].v] = ++tot;T[tot].p = i;T[tot].v = V;
    179         Splay(tot,0);
    180     }else Insert(V,T[i].s[V>T[i].v]);
    181 }
    182  
    183 int main(){
    184     //freopen("pet.in","r",stdin);
    185     //freopen("output.out","w",stdout);
    186     scanf("%d",&n);T[0].v = maxint;
    187     For(i,n){int a,b;
    188         scanf("%d%d",&a,&b);
    189         if(a){
    190             if(nowa){
    191                 nowa--;
    192                 ans = ( ans % Mod + abs(Find(b,root) - b) % Mod ) % Mod;
    193             }
    194             else {
    195                 Insert(b,root);
    196                 nowb++;
    197             }
    198         }else{
    199             if(nowb){
    200                 nowb--;
    201                 ans = ( ans % Mod + abs(Find(b,root) - b) % Mod ) % Mod;
    202             }
    203             else{
    204                 Insert(b,root);
    205                 nowa++;
    206             }
    207         }
    208     }
    209     printf("%d
    ",ans%Mod);
    210     return 0;
    211 }

  • 相关阅读:
    Linux-进程描述(1)—进程控制块
    C++中的继承(2)类的默认成员
    Linux系统date命令的参数及获取时间戳的方法
    new/new[]和delete/delete[]是如何分配空间以及释放空间的
    golang垃圾回收
    golang内存分配
    go中的关键字-reflect 反射
    go中的关键字-go(下)
    go中的关键字-go(上)
    go中的关键字-defer
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3850934.html
Copyright © 2011-2022 走看看