码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 题目3481 Double Queue(SBT ro map)

时间:2015-08-13 22:17:22      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

Double Queue
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11824   Accepted: 5385

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

杭电上的原题,之前用map做的,学了SBT之后再poj用SBT做了一下

注释部分是找前驱版本的删除,,看上去貌似稍慢了一些

ac代码

#include<stdio.h>
#include<string.h>
//#pragma comment(linker, "/STACK:102400000,102400000")
struct s  
{  
    int key,left,right,size,num;  
}tree[1000100];
int top,root,keynum; 
void left_rot(int &x)  
{  
    int y=tree[x].right;  
    tree[x].right=tree[y].left;  
    tree[y].left=x;  
    tree[y].size=tree[x].size;  
    tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;  
    x=y;  
} 
void right_rot(int &x)   
{  
    int y=tree[x].left;  
    tree[x].left=tree[y].right;  
    tree[y].right=x;  
    tree[y].size=tree[x].size;  
    tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;  
    x=y;  
}
void maintain(int &x,bool flag)//维护SBT状态   
{  
    if(flag==false) 
    {  
        if(tree[tree[tree[x].left].left].size>tree[tree[x].right].size)  
            right_rot(x);  
        else  
            if(tree[tree[tree[x].left].right].size>tree[tree[x].right].size)   
            {  
                left_rot(tree[x].left);  
                right_rot(x);     
            }  
            else  
                return;  
    }  
    else
    {  
        if(tree[tree[tree[x].right].right].size>tree[tree[x].left].size)  
            left_rot(x);  
        else  
            if(tree[tree[tree[x].right].left].size>tree[tree[x].left].size)   
            {  
                right_rot(tree[x].right);  
                left_rot(x);  
            }  
            else  
                return;  
    }  
    maintain(tree[x].left,false);  
    maintain(tree[x].right,true);  
    maintain(x,true);  
    maintain(x,false);  
}  
void insert(int &x,int key,int num)  
{  
    if(x==0)  
    {  
        x=++top;  
        tree[x].left=0;  
        tree[x].right=0;  
        tree[x].size=1;  
        tree[x].key=key;
		tree[x].num=num;
    }  
    else  
    {  
        tree[x].size++;  
        if(key<tree[x].key)  
            insert(tree[x].left,key,num);  
        else  
            insert(tree[x].right,key,num);  
        maintain(x,key>=tree[x].key);  
    }  
}  
int getmin(int x) 
{  
    while(tree[x].left)  
        x=tree[x].left;
	//keynum=tree[x].key;
    return x;  
}  
int getmax(int x)  
{  
    while(tree[x].right)  
        x=tree[x].right;
	keynum=tree[x].key;
    return x;  
}
/*int remove(int &x,int key)  
{  
    if(!x)return 0;  
    int res=0;  
    tree[x].size--;  
    if(key==tree[x].key||(key<tree[x].key&&tree[x].left==0)||  
       (key>tree[x].key&&tree[x].right==0))  
    {  
        if(tree[x].left&&tree[x].right)  
        {  
            int p=remove(tree[x].left,key+1);  
            tree[x].key=tree[p].key;  
            res=p;  
        }  
        else  
        {  
            int p=x;  
            x=tree[x].left+tree[x].right;  
            res=p;  
        }  
    }  
    else  
        res=remove(key<tree[x].key?tree[x].left:tree[x].right,key);  
    maintain(x,key<tree[x].key);  
    return res;  
} */
int remove(int &x,int key)  
{  
    tree[x].size--;  
    if(key>tree[x].key)  
        remove(tree[x].right,key);  
    else  
        if(key<tree[x].key)  
            remove(tree[x].left,key);  
        else  
            if(tree[x].left!=0&&tree[x].right==0)  
            {  
                int temp=x;  
                x=tree[x].left;  
                return temp;  
            }  
            else  
                if(!tree[x].left&&tree[x].right!=0)  
                {  
                    int temp=x;  
                    x=tree[x].right;  
                    return temp;  
                }  
                else  
                    if(!tree[x].left&&!tree[x].right)   
                    {  
                        int temp=x;  
                        x=0;  
                        return temp;  
                    }  
                    else
                    {  
                        int temp=tree[x].right;  
                        while(tree[temp].left)  
                            temp=tree[temp].left;  
                        tree[x].key=tree[temp].key;  
                        remove(tree[x].right,tree[temp].key);  
                    }  
}
int main()
{
	int n;
	root=top=0;
	while(scanf("%d",&n)!=EOF,n)
	{
		if(n==1)
		{
			int key,num;
			scanf("%d%d",&num,&key);
			insert(root,key,num);
		}
		else
			if(n==2)
			{
				int x=getmax(root);
				printf("%d\n",tree[x].num);
				remove(root,tree[x].key);
			}
			else
				if(n==3)
				{
					int x=getmin(root);
					printf("%d\n",tree[x].num);
					remove(root,tree[x].key);
				}
	}
}

map版本

ac代码

Problem: 3481  User: kxh1995 
Memory: 196K  Time: 297MS 
Language: C++  Result: Accepted 

Source Code 
#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
using namespace std;
map<int,int>m;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF,n)
    {
        int k,p;
        if(n==1)
        {
            scanf("%d%d",&k,&p);
            m.insert(pair<int,int>(p,k));
        }
        else
            if(n==2)
            {
                if(m.empty())
                {
                    printf("0\n");
                }
                else
                {
                    printf("%d\n",m.rbegin()->second);
                    m.erase(m.rbegin()->first);
                }
            }
            else
                if(n==3)
                {
                    if(m.empty())
                    {
                        printf("0\n");
                    }
                    else
                    {
                        printf("%d\n",m.begin()->second);
                        m.erase(m.begin()->first);
                    }
                }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 题目3481 Double Queue(SBT ro map)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/47622131

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!