POJ 3481 Double Queue (treap模板)
Description
The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:
0 | The system needs to stop serving |
1 K P | Add client K to the waiting list with priority P |
2 | Serve the client with the highest priority and drop him or her from the waiting list |
3 | Serve the client with the lowest priority and drop him or her from the waiting list |
Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.
Input
Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.
Output
For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.
Sample Input
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample Output
0
20
30
10
0 每个顾客有个编号和优先级,银行每次可以添加顾客的要求进队列,且保证队列中当前任意顾客的编号和优先级都不同.银行可以执行先服务最大优先级的顾客或者先服务最小优先级的顾客操作.对于每个服务,输出顾客的编号.
按照优先级插入treap,左儿子优先级大于父亲,右儿子小于
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include <algorithm>
using namespace std;
const int maxn=1e5+;
struct Node
{
Node *ch[];
int r;
int v;
int id;
Node(int v,int id):v(v),id(id){ch[]=ch[]=NULL;r=((rand()<<)|rand()); }
inline int cmp(int x) const {
if (x==v) return -;
else return x>v?:;
}
};
void rotate(Node* &o,int d) {
Node* k=o->ch[d^];
o->ch[d^]=k->ch[d];
k->ch[d]=o;
o=k;
}
void insert(Node* &o,int x,int id) {
if(o==NULL) {
o=new Node(x,id);
}
else {
int d=x<o->v?:;
insert(o->ch[d],x,id);
if(o->ch[d]->r > o->r)
rotate(o,d^);
}
}
void remove(Node* &o,int x) {
int d=o->cmp(x);
if(d==-) {
Node* u=o;
if(o->ch[]!=NULL&&o->ch[]!=NULL) {
int d2=(o->ch[]->r>o->ch[]->r?:);
rotate(o,d2);
remove(o->ch[d2],x);
}
else {
if(o->ch[]==NULL) o=o->ch[];
else o=o->ch[];
delete u;
}
}
else {
remove(o->ch[d],x);
}
}
int find_max(Node *o)
{
if (o->ch[]==NULL){
printf("%d\n",o->id);
return o->v;
}
return find_max(o->ch[]);
}
int find_min(Node *o)
{
if (o->ch[]==NULL){
printf("%d\n",o->id);
return o->v;
}
return find_min(o->ch[]);
} int main()
{
//freopen("de.txt","r",stdin);
Node *root=NULL;
int op;
while (~scanf("%d",&op)){
if (op==) break;
if (op==){
int id,v;
scanf("%d%d",&id,&v);
//printf("ins %d %d \n",id,v);
insert(root,v,id);
}
else if (op==){
if (root==NULL) {
printf("0\n");
continue;
}
int v = find_max(root);
remove(root,v);
}
else if (op==){
if (root==NULL) {
printf("0\n");
continue;
}
int v = find_min(root);
remove(root,v);
}
}
return ;
}
最新文章
- 微信公众号开发(一)--验证服务器地址的Java实现
- Sql--order by、desc降序、top
- Power BI for Office 365(二)Power Query
- 用c#开发微信 (7) 微渠道 - 推广渠道管理系统 2 业务逻辑实现
- HTML5 canvas globalCompositeOperation绘图类型讲解
- [JS]Javascript的this用法
- java 图片处理
- 如何将松散的dll打包进需要发布的exe
- SpringMVC存取Session的两种方法 转
- SSIS:捕获修改了的数据
- ASP.NET - 使用MqSql数据库
- Day034--Python--锁, 信号量, 事件, 队列, 生产者消费者模型, joinableQueue
- Surrounded Regions - LeetCode
- docker push images login -u harbor 问题记录 https 证书
- HFDS核心技术
- .net配置404错误页面
- nmap速查表v1.0(中文版)
- MvvmLight - ViewModelLocator
- FBV和CBV的差异
- Spoken English Practice(appetite,ache,meds,change, stop)Look like you got your appetite back, that&#39;s a good sign.