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

poj 3481 Double Queue STL中map的运用

时间:2015-01-15 09:27:27      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:poj   算法   

题意:

维护一个集合,操作有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;	
} 

poj 3481 Double Queue STL中map的运用

标签:poj   算法   

原文地址:http://blog.csdn.net/sepnine/article/details/42719515

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