Double Queue POJ - 3481(Splay、Set)

1 篇文章 0 订阅

题意:
有3种操作,op=1时,添加一个优先级为p,编号为k的客户,op=2时表示为优先级最大的客户服务,op=3则为优先级最小的客户服务


题解:
这题连数据范围都没给,而且还是POJ的题,真的难受…

先看Set做法,直接放入pair即可,set会先按照first排序,再按照second排序,所以直接取头或尾即可
Set天下第一!!!

再看Splay做法,码量多了好多
可以考虑用Splay维护优先级值,每次查询的最大值一定在右子树的右子树的…的右儿子上,最小值同理左子树…而且还没有同一权值的情况,比模板简单多了


AC代码(Splay版):

#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN = 2e5+50;
const int INF = 1e9;
struct node{ int son[2],fa,val,id; }t[MAXN];
int root,tot,op;
inline int id(int x){ return x==t[t[x].fa].son[1]; }
inline void rotate(int x){
    int y=t[x].fa,z=t[y].fa,k=id(x);
    t[z].son[id(y)]=x; t[x].fa=z;
    t[y].son[k]=t[x].son[k^1]; t[t[x].son[k^1]].fa=y;
    t[x].son[k^1]=y; t[y].fa=x;
}
inline void splay(int x,int pos){
    while(t[x].fa!=pos){
        int y=t[x].fa,z=t[y].fa;
        if(z!=pos) id(x)==id(y) ? rotate(y):rotate(x);
        rotate(x);
    }
    if(!pos) root=x;
}
inline void Insert(int k,int x){
    int u=root,fa=0;
    while(u && t[u].val!=x) { fa=u; u=t[u].son[x>t[u].val]; }
    u = ++tot;
    if(fa) t[fa].son[x>t[fa].val]=u;
    t[u].son[0]=t[u].son[1]=0;
    t[u].fa=fa; t[u].val=x; t[u].id=k;
    splay(u,0);
}
inline int Find_max(){
    int u=root;
    while(t[u].son[1]) u=t[u].son[1];
    if(u==root) root=t[u].son[0];
    int fa=t[u].fa;
    t[fa].son[1]=t[u].son[0]; t[t[u].son[0]].fa=fa;
    return t[u].id;
}
inline int Find_min(){
    int u=root;
    while(t[u].son[0]) u=t[u].son[0];
    if(u==root) root=t[u].son[1];
    int fa=t[u].fa;
    t[fa].son[0]=t[u].son[1]; t[t[u].son[1]].fa=fa;
    return t[u].id;
}
int main(){
    //freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    while(~scanf("%d",&op) && op){
        if(op==1){
            int k,x; scanf("%d%d",&k,&x);
            Insert(k,x);
        }else if(op==2){
            printf("%d\n",Find_max());
        }else if(op==3){
            printf("%d\n",Find_min());
        }
    }
    return 0;
}


AC代码(Set版):

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
const int MAXN = 2e5+50;
const int INF = 1e9;
set<pair<int,int> > s; int op;
set<pair<int,int> >::iterator it;
int main(){
    //freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    while(~scanf("%d",&op) && op){
        if(op==1){
            int k,x; scanf("%d%d",&k,&x);
            s.insert(make_pair(x,k));
        }else if(op==2){
            if(s.empty()) puts("0");
            else{
                it=--s.end();
                printf("%d\n",(*it).second);
                s.erase(it);
            }
        }else if(op==3){
            if(s.empty()) puts("0");
            else{
                it=s.begin();
                printf("%d\n",(*it).second);
                s.erase(it);
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值