Kiki is considered as a smart girl in HDU, many boys fall in love with her! Now, kiki will finish her education, and leave school, what a pity! One day, zjt meets a girl, who is like kiki very much, in the campus, and calls her little kiki. Now, little kiki want to design a container, which has two kinds of operation, push operation, and pop operation.
Push M:
Push the integer M into the container.
Pop M:
Find the maximal integer, which is not bigger than M, in the container. And pop it from the container. Specially, for all pop operations, M i i is no bigger than M i+1 i+1.
Although she is as smart as kiki, she still can't solve this problem! zjt is so busy, he let you to help little kiki to solve the problem. Can you solve the problem?
Push M:
Push the integer M into the container.
Pop M:
Find the maximal integer, which is not bigger than M, in the container. And pop it from the container. Specially, for all pop operations, M i i is no bigger than M i+1 i+1.
Although she is as smart as kiki, she still can't solve this problem! zjt is so busy, he let you to help little kiki to solve the problem. Can you solve the problem?
9 Push 10 Push 20 Pop 2 Pop 10 Push 5 Push 2 Pop 10 Pop 11 Pop 19 3 Push 2 Push 5 Pop 2
No Element! 10 5 2 No Element! 2
题意:要求输入N条指令,有两种操作:1. Push k将数字k加入容器中;2.Pop k从容器中取出小于等于k的最大的数。
坑点:1.一开始使用的是set,但会造成重复输入会覆盖之前存在的数据,所以用multiset;
2.每组操作之间需换行;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
using namespace std;
int main()
{
int t,x;char str[5];
multiset<int>cot;
while(~scanf("%d",&t))
{
cot.clear();
while(t--)
{
scanf("%s",str);
scanf("%d",&x);
if(str[1]=='u')
cot.insert(x);
else
{
//printf("Arrive!\n");
if(cot.size()==0||*cot.begin()>x)
{
printf("No Element!\n");
continue;
}
multiset<int>::iterator p=cot.upper_bound(x);
//printf("P: %d\n",*p);
printf("%d\n",*(--p));
cot.erase(p);
continue;
}
}
printf("\n");
}
return 0;
}