MZL's simple problem
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 297 Accepted Submission(s): 134
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
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
set<int>d;
int main()
{
int n;
scanf("%d", &n);
d.clear();
while (n--)
{
int m;
scanf("%d", &m);
if (m == 1)
{
int t;
scanf("%d", &t);
d.insert(t);
}
if (m == 2)
{
if (!d.empty())
d.erase(d.begin());
}
if (m == 3)
{
if (d.empty())
printf("0\n");
else
printf("%d\n", *(--d.end()));
}
}
return 0;
}
1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就
2,Set中的元素可以是任意类型的,但是由于需要排序,所以元素必须有一个序,即大小的比较关系,比如
3,自定义比较函数;
4,set的基本操作:
begin()
clear()
count()
empty()
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
{
bool operator()(const newtype &a, const newtype &b) const
{
return a.s<b.s;
}
};//the “; ”
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<<" ";
}
element自动排序是按照char s的大小排序的;
6.其他的set构造方法;
#include <iostream>
#include <set>
using namespace std;
bool fncomp (int lhs, int rhs) {return lhs<rhs;}
struct classcomp {
};
int main ()
{