zoukankan      html  css  js  c++  java
  • 刷题总结——宠物收养所(bzoj1208)

    题目:

    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,最后一个领养者没有宠物可以领养)
    数据有一定梯度
    对于80%的数据,收养者按照到来的顺序收养宠物

    题解:

    splay模板题··当然用set做更简单···,按照题意往splay树种加入人或宠物的特殊值,按照题意加入后要么树种全是宠物或人的特殊值,此时直接继续加入操作;要么有一个人的特殊值剩下的全是宠物,或者有一个宠物的特殊值剩下的全是人,因此直接按照题意删除一个人和一个宠物(即一次领养)即可;注意用cnt1和cnt0来记录人和宠物在树中的数量

    代码:

    1.splay

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<cstring>
    #include<string>
    #include<algorithm>
    using namespace std;
    const int N=8e4+5;
    const int mod=1000000;
    int root,tot,size[N],son[N][2],father[N],key[N];
    int n,cnt0=0,cnt1=0,a,b;
    long long ans=0;
    inline int R()
    {
      char c;int f=0;
      for(c=getchar();c<'0'||c>'9';c=getchar());
      for(;c<='9'&&c>='0';c=getchar())
        f=(f<<3)+(f<<1)+c-'0';
      return f;
    }
    long long Abs(long long x)
    {
      return x<0?-x:x;
    }
    inline void update(int now) 
    {
      if(now)
      {
        size[now]=1;
        if(son[now][0])  size[now]+=size[son[now][0]];
        if(son[now][1])  size[now]+=size[son[now][1]];
      }
    }
    inline void clear(int now)
    {
      size[now]=son[now][1]=son[now][0]=father[now]=key[now]=0;
    }
    inline int get(int now)
    {
      return son[father[now]][1]==now;
    }
    inline void rotate(int now)
    {
      int fa=father[now],ofa=father[fa],which=get(now);
      son[fa][which]=son[now][which^1],father[son[fa][which]]=fa; 
      son[now][which^1]=fa,father[fa]=now,father[now]=ofa;
      if(ofa)  son[ofa][son[ofa][1]==fa]=now;
      update(fa),update(now);
    }
    inline void splay(int now) 
    { 
      while(father[now]) 
      {  
        if(father[father[now]]) 
          rotate(get(now)==get(father[now])?father[now]:now);
        rotate(now);
      }
      root=now;
    }
    inline void find(int x)
    {
      int now=root;
      while(true)
      {
        if(key[now]==x)  {splay(now);break;}
        else if(x>key[now])  now=son[now][1];
        else now=son[now][0];
      }
    }
    inline void insert(int x)
    {
      int now=root,last=0;
      while(true)
      {
        if(!now)
        {
          now=++tot;size[now]=1;father[now]=last,key[now]=x;
          son[last][x>key[last]]=now;update(last);
          splay(now);
          break;
        }
        last=now;
        now=son[now][x>key[now]];
      }
    }
    inline int pre()
    {
      int now=son[root][0];
      while(son[now][1])  now=son[now][1];
      return now;
    }
    inline int next()
    {
      int now=son[root][1];
      while(son[now][0])  now=son[now][0];
      return now;
    }
    inline long long prex()
    {
      int now=son[root][0];
      if(!now)  return 1e+18;
      while(son[now][1])  now=son[now][1];
      return key[now];
    }
    inline long long nextx()
    {
      int now=son[root][1];
      if(!now)  return 1e+18;
      while(son[now][0])  now=son[now][0];
      return key[now];
    }
    inline void Delete(int x)
    {
      if(x==1e+18) return;
      find(x);
      if(!son[root][0]&&!son[root][1]){clear(root);root=0;return;}
      else if(!son[root][0]){int oldroot=root;root=son[oldroot][1];father[root]=0;clear(oldroot);return;}
      else if(!son[root][1]){int oldroot=root;root=son[oldroot][0];father[root]=0;clear(oldroot);return;}
      else
      {
        int leftbig=pre(),oldroot=root;
        splay(leftbig);
        son[root][1]=son[oldroot][1];father[son[root][1]]=root;
        clear(oldroot);update(root);
        return;
      }
    }
    int main()
    {
      //freopen("a.in","r",stdin);
      n=R();  
      for(int i=1;i<=n;i++)
      {
        a=R(),b=R();
        if(!cnt0&&!cnt1)
        {
          if(a==0) cnt0++;
          else cnt1++;
          insert(b);
        }
        else if(!cnt0&&a==1)
        {
          cnt1++;insert(b);
        }
        else if(!cnt1&&a==0)
        {
          cnt0++;insert(b);
        }
        else
        {
          if(!cnt1)  cnt0--;
          else if(!cnt0)  cnt1--;
          insert(b);
          long long temp1=prex();      
          long long temp2=nextx();
          long long ans1=Abs(temp1-b);
          long long ans2=Abs(temp2-b);
          if(ans1==ans2)  Delete(temp1),Delete(b);
          if(ans1>ans2)  Delete(temp2),Delete(b);
          if(ans1<ans2)  Delete(temp1),Delete(b);
          ans=(ans+min(ans1,ans2))%mod;
        }
      }
      cout<<ans<<endl;
      return 0;
    }
     

     2.set

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<set>
    using namespace std;
    const int N=8e4+5;
    const int mod=1000000;
    const int inf=1e+9;
    set<int>st;
    int n,cnt0=0,cnt1=0,a,b;
    long long ans=0;
    inline int R()
    {
      char c;int f=0;
      for(c=getchar();c<'0'||c>'9';c=getchar());
      for(;c<='9'&&c>='0';c=getchar())
        f=(f<<3)+(f<<1)+c-'0';
      return f;
    }
    long long Abs(long long x)
    {
      return x<0?-x:x;
    }
    int main()
    {
      //freopen("a.in","r",stdin);
      n=R();  
      st.insert(-inf);
      st.insert(inf);
      for(int i=1;i<=n;i++)
      {
        a=R(),b=R();
        if(!cnt0&&!cnt1)
        {
          if(a==0) cnt0++;
          else cnt1++;
          st.insert(b);
        }
        else if(!cnt0&&a==1)
        {
          cnt1++;st.insert(b);
        }
        else if(!cnt1&&a==0)
        {
          cnt0++;st.insert(b);
        }
        else
        {
          if(!cnt1)  cnt0--;
          else if(!cnt0)  cnt1--;
           set<int>::iterator l=--st.lower_bound(b),r=st.lower_bound(b);
          if(b-*l<=*r-b&&*l!=-inf)
          {
            ans+=b-*l;
            st.erase(l);
          }
          else 
          {
            ans+=*r-b;
            st.erase(r);
          }
          ans%=mod;
        }
      }
      printf("%d",ans);
      return 0;
    }
  • 相关阅读:
    如何解决无法成功git commit 和git push
    orleans 项目调试注意
    silo 主机 配置
    asp.net core 项目引用包版本问题
    C# async 方法怎么被正确的消费 (新篇)
    C# 虚方法 复习
    C# dynamic 适用场景进一步说明
    [MySQL]
    C# Subject 观察者模式
    C# 协变与逆变
  • 原文地址:https://www.cnblogs.com/AseanA/p/7489032.html
Copyright © 2011-2022 走看看