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

poj 3481 Double Queue STL中map的运用

2015-01-14 22:46 387 查看
题意:

维护一个集合,操作有1:加入一个元素,2:删除最大元素,3:删除最小元素。

分析:

map本质是个容器,且具有第一个关键字有序的性质,所以用它来水水就好啦~

代码:
//poj 3481
//sep9
#include <iostream>
#include <map>
using namespace std;

map<int,int> mymap;
map<int,int>::iterator iter; 

int main()
{
	int x,sum=0;
	while(scanf("%d",&x)==1&&x){
		if(x==1){
			int a,b;
			scanf("%d%d",&a,&b);
			mymap[b]=a;
			++sum;
		}else if(x==2){
			if(sum==0)
				printf("0\n");
			else{
				iter=mymap.end();
				--iter;
				printf("%d\n",iter->second);
				mymap.erase(iter);
				--sum;
			}		
		}else if(x==3){
			if(sum==0)
				printf("0\n");
			else{
				iter=mymap.begin();
				printf("%d\n",iter->second);
				mymap.erase(iter);
				--sum;
			}			
		}	
	}
	return 0;	
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: