Yuna's confusion
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 15
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Although yuna is very intelligent, she can not think of how to do it, can you help her to solve this problem?
Input
If s is “Add”, then there will be an integer i (0 <i <100000), means press element i into Container.
If s is “Del”, then there will be an integer i (0 <i <100000), indicated that delete the element i from the container
If s is “Query”, then there will be two integers x and k (0 <x <100000, 0 <k <10000),means the inquiries, the element is greater than x, and the k-th larger number.
Output
Sample Input
5 Add 5 Del 2 Add 6 Query 3 2 Query 8 1 7 Add 2 Add 2 Add 4 Query 1 1 Query 1 2 Query 1 3 Query 1 4 5 Add 5 Add 5 Del 5 Del 5 Query 1 1
Sample Output
No Elment! 6 Not Find! 2 2 4 Not Find! Not Find!代码:
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
multiset<int> S; //multiset可存放重复的元素,set不能存放重复的元素,已排好序;
multiset<int>::iterator it;
int main()
{
int n;
char ss[10];
int a,b;
while(scanf("%d",&n)!=-1)
{
S.clear();
for(int i = 1; i <= n; i++)
{
scanf("%s",ss);
if( ss[0] == 'A' )
{
scanf("%d",&a);
S.insert(a);
}
else if( ss[0] == 'D' )
{
scanf("%d",&a);
int cnt = S.count(a);//返回当前集合中出现的某个值的元素的数目;
if( cnt == 0 )
{
printf("No Elment!\n");
continue;
}
S.erase(a);//删除等于key值的所有元素;
for(int j = 1; j <= cnt-1; j++)
S.insert(a);
}
else
{
scanf("%d%d",&a,&b);
it = S.upper_bound(a);//在当前多元集合中返回一个指向大于Key值的元素的迭代器;
if( it == S.end() )
{
printf("Not Find!\n");
continue;
}
for(int j=1;it != S.end() && j <b; j++, it++) ;
if( it == S.end() )
{
printf("Not Find!\n");
continue;
}
printf("%d\n", *(it) );
}
}
}
return 0;
}
链接:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=16638&pid=1005