【BZOJ 1208】【HNOI 2004】宠物收养所【treap】|【STL】

11 篇文章 0 订阅
4 篇文章 0 订阅

好久没写treap,手都生了。

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以后的结果。

题解

一、treap,记录当前商店剩下的是人还是宠物。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>

#define mod 1000000
using namespace std;

struct node{
    node *ch[2];
    int r,v,s;
    node(int x){
        v = x;r = rand();s = 1;
        ch[0] = ch[1] = NULL;
    }
    int cmp(int x) {
        if(x == v) return -1;
        else return x > v ? 1 : 0;
    }
    void maintain() {
        s = 1;
        if(ch[1]) s += ch[1]->s;
        if(ch[0]) s += ch[0]->s;
    }
} *root;
int f,n;

void rotate(node* &o,int d)
{
    node* k = o->ch[d^1];
    o->ch[d^1] = k->ch[d];
    k->ch[d] = o;
    o->maintain(); k->maintain();
    o = k;
}

void insert(node* &o,int x)
{
    if(o == NULL) o = new node(x);
    else {
        int d = (x < o->v ? 0 : 1);
        insert(o->ch[d],x);
        if(o->ch[d]->r > o->r) rotate(o,d^1);
    }
    o->maintain();
}

void remove(node* &o,int x)
{
    int d = o->cmp(x);
    if(d == -1) {
        node* u = o;
        if(o->ch[0] && o->ch[1]) {
            int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0);
            rotate(o,d2);remove(o->ch[d2],x);
        }
        else {
            if(o->ch[1]) o = o->ch[1]; else o = o->ch[0];
            delete u;
        }
    } else remove(o->ch[d],x);
    if(o) o->maintain();
}

int res;
void look_before(node* o,int x)
{
    if(o == NULL) return;
    if(o->v < x) {
        res = o->v;
        look_before(o->ch[1],x);
    }else look_before(o->ch[0],x);
}

void look_after(node* o,int x)
{
    if(o == NULL) return;
    if(o->v > x) {
        res = o->v;
        look_after(o->ch[0],x);
    } else look_after(o->ch[1],x);
}

int main()
{
    int opt,x,a,b,sum,ans;
    scanf("%d",&n);
    sum = ans = 0;
    while(n--) {
        scanf("%d%d",&opt,&x);
        if(sum == 0){f = opt; insert(root,x);sum++;}
        else {
            if(opt == f) {insert(root,x);sum++;}
            else
            {
                sum--;
                res = -1;look_before(root,x);a = res;
                res = -1;look_after(root,x); b = res;
                if(a == -1) {ans += abs(b-x); remove(root,b);}
                if(b == -1) {ans += abs(a-x); remove(root,a);}
                if(a != -1 && b != -1)
                    if(abs(b-x) < abs(a-x)) {ans += abs(b-x); remove(root,b);}
                    else {ans += abs(a-x); remove(root,a);}
            }
        }
        ans %= mod;
    }
    printf("%d\n",ans);
    return 0;
}

二、STL也可以(set好妙啊)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>

#define mod 1000000
#define inf 0x7fffffff
using namespace std;
int t,n,ans;
set <int> st;

void query(int x) {
    set<int> :: iterator l = --st.lower_bound(x),r = st.lower_bound(x); 
    if(x - *l <= *r - x && *l != -inf) {
        ans += x - *l;
        st.erase(l);
    }
    else {
        ans += *r-x;
        st.erase(r);
    }
    ans %= mod;
}

int main()
{
    int a,b;
    st.insert(inf); st.insert(-inf);
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) {
        scanf("%d%d",&a,&b);
        if(st.size() == 2) {
            t = a;
            st.insert(b);
        }
        else if(a == t) st.insert(b);
        else query(b);
    }
    printf("%d\n",ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值