Problem Description
After yuna studies the STL container,she finds the STL powerful for AC.For the k-th number, she is also very familiar with it. Now yuna meets a very similar problem, yuna wants to design a container, the container is to support the three operations,including
add,del and query.
Although yuna is very intelligent, she can not think of how to do it, can you help her to solve this problem?

Although yuna is very intelligent, she can not think of how to do it, can you help her to solve this problem?

Input
Input contains multiple test cases.,Each test case the first number is an integer n (1 <= n <100000), means that the number of operation to do. The next m lines, each line will be an string at the beginning, s which has three values:
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.
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
For each deletion, if you want to delete the element which does not exist, the output "No Elment!". For each query, output the suitable answers in line .if the number does not exist, the output "Not Find!".
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!
Author
Source
developing schools contest 5
传说中的水题,正解是用线段树做的,不过用简单的STL水过了.......
谁说vector不可以排序,只要用的好一样能排
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
vector<int> v;
int main()
{
int n;
char job[20];
int x;
int y;
while(scanf("%d%*c",&n)!=EOF)
{
v.clear();
while(n--)
{
vector<int>::iterator it;
scanf("%s",job);
if(strcmp(job,"Add")==0)
{
scanf("%d%*c",&x);
it=upper_bound(v.begin(),v.end(),x);//这个地方对vector进行排序,因为每次插入的时候是不会影响原来的顺序所以,插入之后还是有序的
v.insert(it,x);
}
else if(strcmp(job,"Del")==0)
{
scanf("%d%*c",&x);
it=find(v.begin(),v.end(),x);
if(it==v.end())
{
printf("No Elment!\n");
}
else
{
v.erase(it);//这个地方删除的是一个元素,并不是所有的元素
}
}
else
{
scanf("%d%d",&x,&y);
it=upper_bound(v.begin(),v.end(),x);
if(it==v.end())
{
printf("Not Find!\n");
}
else
{
it+=(y-1);
if(it>=v.end())
{
printf("Not Find!\n");
}
else
printf("%d\n",*it);
}
}
}
}
return 0;
}
本文介绍了一个使用STL容器解决特定问题的例子。问题要求设计一个能够执行添加、删除和查询操作的容器。通过使用vector和其内置的排序及查找功能,有效地实现了需求。示例代码展示了如何利用STL来高效地解决问题。
5324

被折叠的 条评论
为什么被折叠?



