POJ 3481 multimap / 平衡树 / splay

7 篇文章 0 订阅
5 篇文章 0 订阅

题目意思:


0 结束系统

1 K P  把一个叫K的客户,加入系统。他的优先级是P

2  输出最高优先级的客户名字, 同时删掉他

3 输出最低优先级的客户名字,同时删掉他



想法: 平衡树,找最大值最小值即可。 


C++这次我试了试multimap,挺好玩的。实际上这题也应该没重复关键字吧~


iterator 迭代器的begin()肯定是最小值

end() --  肯定是最大值。  空可以用 empty之类判断都可以。 我用的是begin() == end()为空


用STL程序非常简短

时间也很快

3481Accepted776K516MSG++

#include <iostream>
#include <cstdio>
#include <map>
#include <cstdlib>
using namespace std;

multimap<int, int>G;
multimap<int ,int>::iterator it;
int main()
{
	int flag;
	while (1)
	{
		scanf("%d", &flag);	
		if (flag == 1)
		{
			int k, p;
			scanf("%d%d", &k, &p);	
			G.insert(make_pair(p, k));				
		}
		if (flag == 3)
		{
			it = G.begin();	
			if (it == G.end())	printf("0\n");
			else {
				printf("%d\n", it -> second);
				G.erase(it);	
			}
		
		}
		if (flag == 2)
		{
			it = G.end();
			if (G.begin() == it)	printf("0\n");
			else{
				--it;
				printf("%d\n", it -> second);
				G.erase(it);	
			}
		}
		if (flag == 0)	break;
	}
}


=============

然后是自己写的SPLAY,和STL的速度差不多

3481Accepted760K547MSG++


#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;


typedef pair<int, int> PII;
int maxint = 0x7fffffff;

struct node
{
	PII key;
	node *c[2];
	int size;
	node()
	{
		key = make_pair(0, 0);
		size = 0;
		c[0] = c[1] = this;	
	}
	node(PII KEY_, node *c0, node *c1)
	{
		key = KEY_;
		c[0] = c0;
		c[1] = c1;	
	}
	node* rz(){return size = c[0] -> size + c[1] -> size + 1, this;}
}Tnull, *null = &Tnull;

struct splay
{
	node *root;
	splay()
	{
		root = (new node(*null)) -> rz();
		root -> key = make_pair(maxint, maxint);
	}
	void zig(int d)
	{
		node *t = root -> c[d];	
		root -> c[d] = null -> c[d];
		null -> c[d] = root;
		root = t;
	}
	void zigzig(int d)
	{
		node *t = root -> c[d] -> c[d];	
		root -> c[d] -> c[d] = null -> c[d];
		null -> c[d] = root -> c[d];
		root -> c[d] = null -> c[d] -> c[!d];
		null -> c[d] -> c[!d] = root -> rz();
		root = t;
	}
	void finish(int d)
	{
		node *t = null -> c[d], *p = root -> c[!d];	
		while (t != null)
		{
			t = null -> c[d] -> c[d];	
			null -> c[d] -> c[d] = p;	
			p = null -> c[d] -> rz();
			null -> c[d] = t;
		}
		root -> c[!d] = p;
	}

	void select(int k)//谁有k个left儿子
	{
		int t;	
		while (1)
		{
			bool d = k > (t = root -> c[0] -> size);	
			if (k == t || root -> c[d] == null)	break;
			if (d)	k -= t+ 1;
			bool dd = k > (t = root -> c[d] -> c[0] -> size);
			if (k == t || root -> c[d] ->c[dd] == null){zig(d);	break;}
			if (dd)	k -= t+1;
			d != dd ? zig(d), zig(dd) : zigzig(d);
		}
		finish(0), finish(1);
		root -> rz();	
	}
	void search(PII x)
	{
		while (1)	
		{
			bool d = x > (root -> key);	
			if (root -> c[d] == null)break;
			bool dd = x > (root -> c[d] -> key);
			if (root -> c[d] -> c[dd] == null)	{zig(d);break;}
			d != dd ? zig(d), zig(dd) : zigzig(d);
		}
		finish(0), finish(1);
		root -> rz();
		if (x > root -> key)	select(root -> c[0] -> size + 1);
	}
	void ins(PII x)
	{
		search(x);	
		node *oldroot = root;
		root = new node(x, oldroot -> c[0], oldroot);
		oldroot -> c[0] = null;
		oldroot -> rz();
		root -> rz();
	}
	void del(PII x)
	{
		search(x);	
		node *oldroot = root;
		root = root -> c[1];
		select(0);
		root -> c[0] = oldroot -> c[0];
		root -> rz();
		delete oldroot;
	}
	PII sel(int k){return select(k - 1), root -> key;}
	int ran(PII x){return search(x), root -> c[0] -> size + 1;}
}sp;

int n, tmp, ans;
 
int main()
{
	int flag;
	while (1)
	{
		scanf("%d", &flag);	
		if (flag == 1)
		{
			int k, p;//k客户是p
			scanf("%d%d", &k, &p);	
			sp.ins(make_pair(p,k));				
		}
		if (flag == 2)
		{
			if (sp.root->size == 1)	printf("0\n");
			else {
				PII tmp;
				tmp = sp.sel(sp.root -> size - 1);
				printf("%d\n", tmp.second);
				sp.del(tmp);	
			}
		
		}
		if (flag == 3)
		{
			if (sp.root -> size == 1)	printf("0\n");
			else{
				PII tmp;
				tmp = sp.sel(1);
				printf("%d\n", tmp.second);
				sp.del(tmp);	
			}
		}
		if (flag == 0)	break;
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值