zoukankan      html  css  js  c++  java
  • [HNOI 2004]宠物收养场

    Description

    凡凡开了一间宠物收养场。收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。

    每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养场的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养场总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。

    被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。

    收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为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, 最后一个领养者没有宠物可以领养。

    题解

    基本的$Treap$加上找前驱后继节点可以$AC$。
    首先,建一棵$Treap$保存宠物或人,每次在里面找一个最近的抵消(通过找前驱后继来实现),其他的看题都知道怎么处理。
    那么重点来了,前驱后继的实现。
    前驱:
    定义变量$p1$,用来保存前驱最近的节点。
    从根开始(显然),如果需要的编号小于当前编号,就往前走(走左儿子),否则$p1=$当前编号,往右走。

    后继同理。

    做这道题的时候没想到直接去找前驱后继,就去二分找值了...

      1 #include<set>
      2 #include<map>
      3 #include<cmath>
      4 #include<ctime>
      5 #include<queue>
      6 #include<stack>
      7 #include<vector>
      8 #include<cstdio>
      9 #include<string>
     10 #include<cstdlib>
     11 #include<cstring>
     12 #include<iostream>
     13 #include<algorithm>
     14 #define LL long long
     15 #define RE register
     16 #define IL inline
     17 using namespace std;
     18 const int N=80000;
     19 const int MOD=1000000;
     20 
     21 IL int Abs(const int &a) {return a<0 ? -a:a;}
     22 IL int Min(const int &a,const int &b) {return a<b ? a:b;}
     23 
     24 struct node
     25 {
     26     int key,lev,low;
     27     node *child[2];
     28 }T[N+5],*root=NULL,*pos=T;
     29 int key[N+5];
     30 int n,a,b;
     31 int flag,ans,size;
     32 
     33 void NewNode(node* &r,int key);
     34 void Insert(node* &r,int key);
     35 void Delete(node* &r,int key);
     36 int Query(node* r,int rank,int cnt);
     37 void Rotate(node* &r,bool t);
     38 int Count(node* r);
     39 
     40 int main()
     41 {
     42     srand(time(0));
     43     scanf("%d",&n);
     44     while (n--)
     45     {
     46         scanf("%d%d",&a,&b);
     47         if (size==0)
     48         {
     49             flag=a;
     50             Insert(root,b);
     51             size++;
     52         }
     53         else if (flag==a)
     54         {
     55             Insert(root,b);
     56             size++;
     57         }
     58         else
     59         {
     60             int l=1,r=size,mid,ansn=Query(root,1,0),ansi=1;
     61             while (l<=r)
     62             {
     63                 mid=(l+r)>>1;
     64                 int tmp=Query(root,mid,0);
     65                 if (tmp<=b) ansn=tmp,ansi=mid,l=mid+1;
     66                 else r=mid-1;
     67             }
     68             if (ansi==size||size==1)
     69             {
     70                 ans=(ans+Abs(b-ansn))%MOD;
     71                 Delete(root,ansn);
     72                 size--;
     73             }
     74             else 
     75             {
     76                 int tmp=Query(root,ansi+1,0);
     77                 if (Abs(b-ansn)<=Abs(b-tmp))
     78                 {
     79                     ans=(ans+Abs(b-ansn))%MOD;
     80                     Delete(root,ansn);
     81                 }
     82                 else
     83                 {
     84                     ans=(ans+Abs(b-tmp))%MOD;
     85                     Delete(root,tmp);
     86                 }
     87                 size--;
     88             }
     89         }
     90     }
     91     printf("%d
    ",ans);
     92     return 0;
     93 }
     94 
     95 void NewNode(node* &r,int key)
     96 {
     97     r=pos++;
     98     r->key=key;
     99     r->low=1;
    100     r->lev=rand();
    101 }
    102 void Insert(node* &r,int key)
    103 {
    104     if (!r)
    105     {
    106         NewNode(r,key);
    107         return;
    108     }
    109     bool t=key>r->key;
    110     if (!t) r->low++;
    111     Insert(r->child[t],key);
    112     if (r->child[t]->lev<r->lev)
    113     {
    114         Rotate(r,!t);
    115         r->low=1+Count(r->child[0]);
    116     }
    117 }
    118 void Delete(node* &r,int key)
    119 {
    120     if (r->key==key)
    121     {
    122         if (r->child[0]&&r->child[1])
    123         {
    124             bool t=r->child[0]->lev<r->child[1]->lev;
    125             Rotate(r,t);
    126             r->low=1+Count(r->child[0]);
    127             if (!t) r->low--;
    128             Delete(r->child[t],key);
    129         }
    130         else
    131         {
    132             if (r->child[0]) r=r->child[0];
    133             else r=r->child[1];
    134         }
    135     }
    136     else
    137     {
    138         bool t=key>r->key;
    139         if (!t) r->low--;
    140         Delete(r->child[t],key);
    141     }
    142 }
    143 void Rotate(node* &r,bool t)
    144 {
    145     node *y=r->child[!t],*R=r;
    146     r->child[!t]=y->child[t];
    147     y->child[t]=r;
    148     r=y;
    149     R->low=1+Count(R->child[0]);
    150 }
    151 int Query(node* r,int rank,int cnt)
    152 {
    153     if (r->low+cnt==rank) return r->key;
    154     if (r->low+cnt<rank) return Query(r->child[1],rank,r->low+cnt);
    155     if (r->low+cnt>rank) return Query(r->child[0],rank,cnt);
    156 }
    157 int Count(node* r)
    158 {
    159     if (!r) return 0;
    160     return r->low+Count(r->child[1]);
    161 }
  • 相关阅读:
    ASP.NET MVC one view bind many model
    说一说MVC的CustomHandlerErrorAttribute(五)
    今天俺要说一说工厂方法模式(Factory)
    今天俺要说一说简单工厂模式(Simple Factory)
    我对SQL性能优化的看法,对我的文章有提议的欢迎评论!
    Linux 服务管理两种方式service和systemctl
    Linux grep命令
    Linux 守护进程
    linux Ctrl+z和Ctrl+c的区别
    linux系统卡解决方案
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7396517.html
Copyright © 2011-2022 走看看