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
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)
Next N line ,each line contains one or two numbers,describe one operation.
The number in this set is not greater than 109 .
6 1 2 1 3 3 1 3 1 4 3
3 4
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的大小排序的。