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

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 7457  Solved: 2960

    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,最后一个领养者没有宠物可以领养)

    平衡树-Splay

    在不同的时间,树可能有两种不同的形态:存宠物信息的树或者存客人信息的树。

    对于一行新读入,如果读入物的属性和树属性相同,则将插入树中,否则从树中匹配一个最优位置,并将该位置从树中删除。

    Splay维护树上信息,查找前驱后继即可。

    花了一个小时艰难地敲对splay代码,提交以后一看,3000+ms,别人都是100+ms

    常数写挂了,感到十分害怕。

    花了近两个小时调试各种地方,感觉都没有问题,这时突然想到是不是splay转少了?

    ↑果不其然,在132行的位置 Splay(cnt)写成了Splay(root),这是道splay裸题,数据肯定会卡长链……改完之后妥妥100ms跑完

    ↑喜闻乐见浪费青春2333333333

      1 /*by SilverN*/
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<cstring>
      5 #include<cstdio>
      6 #define LL long long
      7 using namespace std;
      8 const int INF=2100000000;
      9 const int mod=1000000;
     10 const int mxn=100010;
     11 int read(){
     12     int x=0,f=1;char ch=getchar();
     13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     14     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
     15     return x*f;
     16 }
     17 int n;
     18 LL ans;
     19 struct node{
     20     int ch[2],fa;
     21     int v;
     22 }t[mxn];
     23 int cnt=0,root=0;
     24 void newnode(int &rt,int w,int fa){
     25     rt=++cnt;
     26     t[rt].ch[0]=t[rt].ch[1]=0;
     27     t[rt].fa=fa;
     28     t[rt].v=w;
     29     return;
     30 }
     31 void rotate(int x,int kind){
     32     int y=t[x].fa;int z=t[y].fa;
     33     t[y].ch[1^kind]=t[x].ch[kind];
     34     if(t[x].ch[kind]) t[t[x].ch[kind]].fa=y;
     35     t[x].fa=z;
     36     if(z)t[z].ch[t[z].ch[1]==y]=x;
     37     t[y].fa=x;t[x].ch[kind]=y;
     38     return;
     39 }
     40 void Splay(int rt){
     41     while(t[rt].fa){
     42         int y=t[rt].fa; int z=t[y].fa;
     43         if(!z)rotate(rt,t[y].ch[1]!=rt);
     44         else{
     45             if(t[z].ch[0]==y){
     46                 if(t[y].ch[0]==rt){rotate(y,1);rotate(rt,1);}
     47                 else{rotate(rt,0);rotate(rt,1);}
     48             }
     49             else{
     50                 if(t[y].ch[1]==rt){rotate(y,0);rotate(rt,0);}
     51                 else{rotate(rt,1);rotate(rt,0);}
     52             }
     53         }
     54     }
     55     root=rt;
     56     return;
     57 }
     58 void insert(int v){//插入值 
     59     if(!root){newnode(root,v,0);return;}//建立新结点 
     60     int now=root;
     61     while(1){
     62         if(v>t[now].v){
     63             if(t[now].ch[1]==0){newnode(t[now].ch[1],v,now);return;}
     64             else now=t[now].ch[1];
     65         }
     66         else{
     67             if(t[now].ch[0]==0){newnode(t[now].ch[0],v,now);return;}
     68             else now=t[now].ch[0];
     69         }
     70     }
     71     return;
     72 }
     73 void Del(int x){
     74     Splay(x);
     75     //将x提为根结点 
     76     if(!t[x].ch[1] && !t[x].ch[0]){root=0;return;}
     77     //x没有子树,删除之后树变成空树
     78     if(!t[x].ch[0]){root=t[x].ch[1];t[root].fa=0;return;}//x没有左子树,直接删除 
     79     if(!t[x].ch[1]){root=t[x].ch[0];t[root].fa=0;return;}//x没有右子树,直接删除 
     80     int tmp=t[x].ch[0];
     81     while(t[tmp].ch[1])tmp=t[tmp].ch[1];//找到x左子树的最右结点 
     82     t[t[x].ch[0]].fa=0;
     83     Splay(tmp);
     84     t[tmp].ch[1]=t[x].ch[1];
     85     t[t[x].ch[1]].fa=tmp;
     86     root=tmp;
     87     return;
     88 }
     89 int find_pre(int v){
     90     int now=root;
     91     int pos=0,tmp=INF;
     92     while(1){
     93         if(!now)return pos;
     94         if(t[now].v<v && (v-t[now].v<tmp) ){
     95             tmp=v-t[now].v;
     96             pos=now;
     97         }
     98         if(t[now].v<v)now=t[now].ch[1];
     99         else now=t[now].ch[0];
    100     }
    101     return pos;
    102 }
    103 int find_suc(int v){
    104     int now=root;
    105     int pos=0,tmp=INF;
    106     while(1){
    107         if(!now)return pos;
    108         if(t[now].v>v && (t[now].v-v<tmp) ){
    109             tmp=t[now].v-v;
    110             pos=now;
    111         }
    112         if(t[now].v<v)now=t[now].ch[1];
    113         else now=t[now].ch[0];
    114     }
    115     return pos;
    116 }
    117 int main(){
    118     n=read();
    119     int i,j,a,b;
    120     int type=0;
    121     type=read();//初始类型
    122     j=read();//第一个值
    123     newnode(root,j,0);
    124     ans=0;
    125     for(i=2;i<=n;i++){
    126         a=read();b=read();
    127         if(!root){
    128             type=a;//更改当前树类型 
    129             newnode(root,b,0);
    130             continue;
    131         }
    132         if(a==type){insert(b); Splay(cnt);}
    133         else{
    134             int pre=find_pre(b);
    135             int suc=find_suc(b);
    136             if(pre || suc){
    137                 int res1=INF; int res2=INF;
    138                 if(pre)res1=abs(b-t[pre].v);
    139                 if(suc)res2=abs(b-t[suc].v);
    140                 if(res1<=res2){ans=(ans+res1)%mod;Del(pre);}//比较前驱和后继
    141                 else{ans=(ans+res2)%mod;Del(suc);}
    142             }
    143         }
    144     }
    145     printf("%lld
    ",ans);
    146     return 0;
    147 }
  • 相关阅读:
    几个基本trick
    CSP2019 树上的树 口胡
    To do List
    对代码风格的探索(持续更新)
    我回来了。
    年度悲剧
    最短路
    平衡树
    线段树-模板
    jmeter断言之JSON Assertion
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6123101.html
Copyright © 2011-2022 走看看