【POJ3481】Double Queue——伸展树

来源:互联网 发布:python 3.5 2.7 编辑:程序博客网 时间:2024/05/17 17:59

Time Limit: 1000MS Memory Limit: 65536K

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

Source

Southeastern Europe 2007

题意 :题意定义了四种操作
1: 表示有一个编号为k的顾客进入等待的队列,他的优先度为p。
2:处理优先度最高的顾客,并从等待队列中清除,输出其编号,队列为空输出0。
3:处理优先度最低的顾客,并从等待队列中清除,输出其编号,队列为空输出0。
0:表示操作结束。

思路:对于输入的顾客,按照优先度建立二叉查找树,在需要输出的时候,将对应的节点提到根节点,然后将其删掉。

#include <iostream>#include <cstring>#include <cmath>#include <cstdlib>#include <cstdio>#include <queue>#include <stack>#include <algorithm>using namespace std;typedef long long LL;typedef struct node{    int k,p;    node *Lch,*Rch,*fa;} Tr;Tr * Creat(int k,int p)//建立节点{    Tr * P ;    P  = new node;    P->k = k,P->p = p;    P->Lch = P->Rch = P->fa = NULL;    return P;}void Insert(int k,int p,Tr * u)//建立二叉查找树{    if(u->p>p)    {        if(u->Lch == NULL)        {            u->Lch = Creat(k,p);            u->Lch->fa = u;        }        else return Insert(k,p,u->Lch);    }    else    {        if(u->Rch == NULL)        {            u->Rch = Creat(k,p);            u->Rch->fa = u;        }        else Insert(k,p,u->Rch);    }}Tr * FindMax(Tr * u)//最高优先度的点{    if(u->Rch == NULL)        return u;    else return FindMax(u->Rch);}Tr * FindMin(Tr *u)//最低优先度的点。{    if(u->Lch == NULL) return u;    else return FindMin(u->Lch);}void Zig(Tr* u)//右旋{    Tr *p,*fa,*q;    p = u->Lch;    fa = u->fa;    if(fa!= NULL)    {        if(fa->Lch == u)            fa->Lch = p;        else fa->Rch = p;    }    p->fa = fa;    q = p->Rch;    p->Rch = u;    u->fa = p;    u->Lch = q;    if(q != NULL)        q->fa = u;}void Zag(Tr *u)//左旋{    Tr *p,*fa,*q;    p = u->Rch;    fa = u->fa;    q = p->Lch;    if(fa!=NULL)    {        if(fa->Lch==u)            fa->Lch = p;        else fa->Rch = p;    }    p->fa = fa;    p->Lch = u;    u->fa = p;    u->Rch = q;    if(q!=NULL)        q->fa = u;}Tr * Rotate(Tr * u)//提根操作{    Tr *fa,*Gfa;    while(u->fa != NULL)    {        fa = u->fa;        Gfa = fa->fa;        if(Gfa == NULL)        {            if(fa->Lch == u)            {                Zig(fa);            }            else Zag(fa);        }        else        {            if(Gfa->Lch == fa)            {                if(fa->Lch == u)                    Zig(fa);                else Zag(fa);                Zig(Gfa);            }            else            {                if(fa->Lch == u)                    Zig(fa);                else Zag(fa);                Zag(Gfa);            }        }    }    return u;}int main(){    int op,k,p;    Tr *root,*P;    root = NULL;    while(1)    {        scanf("%d",&op);        if(op == 1)        {            scanf("%d %d",&k,&p);            if(root == NULL)            {                root = Creat(k,p);            }            else            {                Insert(k,p,root);            }        }        else if(op == 2)        {            if(root == NULL)            {                printf("0\n");                continue;            }            P = FindMax(root);            P =Rotate(P);            printf("%d\n",P->k);            root = P->Lch;            if(root)                root->fa = NULL;        }        else if(op == 3)        {            if(root == NULL)            {                printf("0\n");                continue;            }            P = FindMin(root);            P = Rotate(P);            printf("%d\n",P->k);            root = P->Rch;            if(root)                root->fa = NULL;        }        else if(op == 0)        {            break;        }    }    return 0;}
0 0
原创粉丝点击