STL之multiset应用。
要求:可添加数,可删除最大、最小的数,可查询当前最大、最小的数。思路:multiset里可以有重复的数,但删除的时候,会把相同的数一并删除,只需将多删的数再加进去即可。
#include<iostream>#include<cstring>#include<cstdio>#include<set>using namespace std;int main(){ int a,b;int c,d; int i=0; char ch; scanf("%d%d",&a,&b); getchar(); multiset <int> tree; tree.clear (); multiset<int >::iteratorit,pre; tree.insert (b); while(i<a) { scanf("%s",ch); if(!strcmp(ch,"A")) { scanf("%d",&c);getchar();tree.insert(c); } else if(!strcmp(ch,"DI")){ int temp=*(tree.begin());int m=tree.count (temp);tree.erase (temp);for(int j=1;j<m;j++)tree.insert (temp); }elseif(!strcmp(ch,"DA")){int temp=*(--tree.end ());int m=tree.count (temp);tree.erase(temp);for(int j=1;j<m;j++)tree.insert (temp);}else if(!strcmp(ch,"Q")){ printf("%d %d\n",*(tree.begin ()),*(--tree.end ()));}i++; } return 0;}
页:
[1]