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

    BZOJ_1208_[HNOI2004]宠物收养所_SPLAY

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

    分析:

    我们记录一下当前的店里是人还是宠物,对于每个匹配操作,我们从根find下去,记录一下最接近它的点,然后删除就行。

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <stdlib.h>
    using namespace std;
    #define N 100050
    #define ls ch[p][0]
    #define rs ch[p][1]
    #define get(x) (ch[f[x]][1]==x)
    int ch[N][2], val[N], siz[N], f[N], sz, cnt[N], tot, n, ans, mod = 1000000, rt;
    void clr(int p) {
        f[p] = ls = rs = siz[p] = val[p] = cnt[p] = 0;
    }
    void pushup(int p) {
        if(!p) return ;
        siz[p] = cnt[p];
        if(ls) siz[p] += siz[ls];
        if(rs) siz[p] += siz[rs];
    }
    void rotate(int x) {
        int y = f[x], z = f[y], k = get(x);
        ch[y][k] = ch[x][!k];
        f[ch[y][k]] = y;
        ch[x][!k] = y;
        f[y] = x;
        f[x] = z;
        if(z) ch[z][ch[z][1] == y] = x;
        pushup(y); pushup(x);
        if(rt == y) rt = x;
    }
    void splay(int x,int y) {
        for(int fa;(fa = f[x]) != y;rotate(x))
            if(f[fa] != y)
                rotate(get(x) == get(fa) ? fa : x);
    }
    void insert(int x) {
        int p = rt, fa = 0;
        if(!rt) { p = ++ tot; cnt[p] = siz[p] = 1; f[p] = ls = rs = 0; val[p] = x; rt = p; return ;}
        while(1) {
            if(val[p] == x) {
                cnt[p] ++ ; pushup(p); pushup(fa); splay(p, 0); return ;
            }
            fa = p;
            p = ch[p][val[p] < x];
            if(!p) {
                p = ++ tot;
                ls = rs = 0;
                val[p] = x;
                cnt[p] = siz[p] = 1;
                f[p] = fa;
                ch[fa][val[fa] < x] = p;
                pushup(p); pushup(fa); splay(p, 0); return ;
            }
        }
    }
    int pre() {
        int p = ch[rt][0];
        while(rs) p = rs;
        return p;
    }
    void del(int p) {
        splay(p, 0);
        if(cnt[p] > 1) {
            cnt[p] -- ;
            pushup(p);
            return ;
        }
        if(!ls && !rs) {
            clr(rt);
            rt = 0;
            return ;
        }
        if(!ls) {
            int tmp = rt; rt = rs; f[rs] = 0; clr(tmp);
            return ;
        }
        if(!rs) {
            int tmp = rt; rt = ls; f[ls] = 0; clr(tmp);
            return ;
        }
        int pr = pre();
        int tmp = rt;
        splay(pr, 0);
        rt = pr;
        ch[pr][1] = ch[tmp][1];
        f[ch[tmp][1]] = pr;
        clr(tmp); pushup(rt); 
    }
    int Abs(int x) {return x>0?x:-x;}
    void solve(int x) {
        int p = rt, re = rt, de = Abs(x - val[rt]), flg = x < val[rt] ? 0 : 1;
        while(1) {
            if(x == val[p]) {
                del(p);
                return ;
            }
            int tmp = ch[p][val[p] < x];
            if(tmp) {
                p = tmp;
                int now = Abs(val[p] - x);
                if(now < de || (now == de && val[p] < x && !flg)) {
                    re = p;
                    de = now;
                    flg = x < val[p] ? 0 : 1;
                } 
            }
            else {
                ans = (ans + de) % mod;
                del(re);
                return ;
            }
        }
    }
    void print(int p) {
        if(!p) return ;
        print(ls);
        printf("p = %d, val[p] = %d, siz[p] = %d, cnt[p] = %d
    ",p,val[p],siz[p],cnt[p]);
        print(rs);
    }
    int main() {
        scanf("%d", &n);
        int i, x, y, now = 0;
        for(i = 1;i <= n; ++ i) {
            scanf("%d%d", &x, &y);
            if(!x) x--;
            if(now == 0) {
                now += x;
                insert(y);
                // print(rt);
            }else if(now > 0) {
                if(x > 0) {
                    insert(y);
                    now ++;
                }else {
                    solve(y);
                    // printf("ans = %d
    ",ans);
                    now --;
                }
                // print(rt);
            }else {
                if(x < 0) {
                    insert(y);
                    now --;
                }else {
                    solve(y);
                    // printf("ans = %d
    ",ans);
                    now ++;
                }
                // print(rt);
            }
        }
        printf("%d
    ",ans);
    }
    
  • 相关阅读:
    Ubuntu 19.04安装phpipam软件
    ubuntu snmp 安装与配置
    xcode 拷贝新的ios image 进去以后 出现 the divices is locked
    常用 Git 命令清单
    ios 从工程中删除Cocoapods
    ios app上架流程
    MySql某一列累计查询
    Docx4j将html转成word时,br标签为软回车的问题修改
    java面试题
    java获取classpath
  • 原文地址:https://www.cnblogs.com/suika/p/8672189.html
Copyright © 2011-2022 走看看