您的位置:首页 > 产品设计 > UI/UE

poj 3481 Double Queue(treap)

2015-11-15 10:21 357 查看
Double Queue

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:

0The system needs to stop serving
1 K PAdd client K to the waiting list with priority P
2Serve the client with the highest priority and drop him or her from the waiting list
3Serve 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


题意:1表示插入客户K,他的优先级是P(相当于大小),2表示输出当前优先级最高的客户(即找出最大值),并且删除。3同理输出最低级的。

treap入门

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int Max=100005;
const int INF=0x3f3f3f3f;
int tot;
int root;
struct TreapTree
{
int id;  //堆依据
int priority; //bst依据
int child[2];
inline void set(int id,int priority)
{
this->id=id;
this->priority=priority;
child[0]=child[1]=0;
}

}T[Max];
//旋转 kind=0 左旋,kind=1右旋 (x为旋转结点的父节点)
void Rotate(int &x,int kind)
{
int y=T[x].child[!kind];
T[x].child[!kind]=T[y].child[kind];
T[y].child[kind]=x;
x=y;
}
void insert(int &x,int id,int priority)
{
if (x==0)
{
T[x=++tot].set(id, priority);
return;
}
else
{
int kind=priority<T[x].priority;
insert(T[x].child[!kind], id, priority);
if (T[x].id<T[T[x].child[!kind]].id)
Rotate(x, kind);
}
}
void remove(int &x,int priority)
{
if (T[x].priority==priority)
{
if (T[x].child[0] && T[x].child[1])
{
int kind=T[T[x].child[0]].id>T[T[x].child[1]].id;
Rotate(x, kind);
remove(T[x].child[kind],priority);
}
else
{
if (!T[x].child[0])
x=T[x].child[1];
else
x=T[x].child[0];
}
}
else
{
int kind=T[x].priority>priority;
remove(T[x].child[!kind],priority);
}
}
int get_obj(int x,int kind)
{
while (T[x].child[kind])
x=T[x].child[kind];
return x;
}
int main()
{

int n,id, priority, x;
tot=0;
root=0;
while(scanf("%d", &n) && n)
{

switch (n)
{
case 1:
scanf("%d%d", &id, &priority);
insert(root, id, priority);
break;
case 2:
x=get_obj(root, 1);
if(x)
{
printf("%d\n",T[x].id);
remove(root, T[x].priority);
}
else
printf("0\n");
break;
case 3:
x=get_obj(root, 0);
if(x)
{
printf("%d\n",T[x].id);
remove(root, T[x].priority);
}
else
printf("0\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: