MZL's simple problem(STL multiset的应用)


Link:http://acm.hdu.edu.cn/showproblem.php?pid=5349


MZL's simple problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1380    Accepted Submission(s): 558


Problem Description
A simple problem
Problem Description
You have a multiple set,and now there are three kinds of operations:
1 x : add number x to set
2 : delete the minimum number (if the set is empty now,then ignore it)
3 : query the maximum number (if the set is empty now,the answer is 0)
 

Input
The first line contains a number  N  ( N106 ),representing the number of operations.
Next  N  line ,each line contains one or two numbers,describe one operation.
The number in this set is not greater than  109 .
 

Output
For each operation 3,output a line representing the answer.
 

Sample Input
  
  
6 1 2 1 3 3 1 3 1 4 3
 

Sample Output
  
  
3 4
 

Source
 


编程思想:由题意可知,利用STL中的multiset容器即可实现对元素的增删查操作。自定义函数缺省时,set和multiset容器里的元素是按照字典序大小从小到大的顺序排序的。

AC code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set> 
#define LL long long 
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
multiset<int>se;
multiset<int>::iterator it;
int main()
{
	//freopen("D:\in.txt","r",stdin);
	int t,x,mm,mi,c;
	scanf("%d",&t);
	se.clear();
	while(t--)
	{
		scanf("%d",&c);
		if(c==1)
		{
			scanf("%d",&x);
			se.insert(x);
		}
		else if(c==2)
		{
			if(!se.empty())
			{
				it=se.begin();
				se.erase(*it);
			}
		}
		else if(c==3)
		{
			if(!se.empty())
			{
				it=se.end();
				it--;
				printf("%d\n",*it);
			}
			else
			{
				printf("0\n");
			}
		}
	}
	return 0;
}


附上set的基本操作:

begin() 返回指向第一个元素的迭代器
clear() 清除所有元素
count() 返回某个值元素的个数
empty() 如果集合为空,返回true
end() 返回指向最后一个元素的迭代器
equal_range() 返回集合中与给定值相等的上下限的两个迭代器
erase() 删除集合中的元素
find() 返回一个指向被查找到元素的迭代器
get_allocator() 返回集合的分配器
insert() 在集合中插入元素
lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
key_comp() 返回一个用于元素间值比较的函数
max_size() 返回集合能容纳的元素的最大限值
rbegin() 返回指向集合中最后一个元素的反向迭代器
rend() 返回指向集合中第一个元素的反向迭代器
size() 集合中元素的数目
swap() 交换两个集合变量
upper_bound() 返回大于某个值元素的迭代器
value_comp() 返回一个用于比较元素间的值的函数
5,自定义比较函数:

For example:

#include<iostream>
#include<set>
using namespace std;
typedef struct {
	int a,b;
	char s;
}newtype;
struct compare //there is no ().
{
	bool operator()(const newtype &a, const newtype &b) const
	{
	return a.s<b.s;
	}
};//the “; ” is here;
set<newtype,compare>element;
int main()
{
	newtype a,b,c,d,t;
	a.a=1; a.s='b';
	b.a=2; b.s='c';
	c.a=4; c.s='d';
	d.a=3; d.s='a';
	element.insert(a);
	element.insert(b);
	element.insert(c);
	element.insert(d);
	set<newtype,compare>::iterator it;
	for(it=element.begin(); it!=element.end();it++)
	cout<<(*it).a<<" ";
	cout<<endl;
	for(it=element.begin(); it!=element.end();it++)
	cout<<(*it).s<<" ";
}
运行结果如下:

3 1 2 4

a b c d

运行结果可看出,element自动排序是按照char s的大小排序的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值