Yuna's confusion

来源:互联网 发布:医学影像专业英语软件 编辑:程序博客网 时间:2024/06/04 18:08

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?

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.

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

5Add 5Del 2Add 6Query 3 2Query 8 17Add 2Add 2Add 4Query 1 1Query 1 2Query 1 3Query 1 45Add 5Add 5Del 5Del 5Query 1 1

Sample Output

No Elment!6Not Find!224Not Find!Not Find!

Author

Rexdf

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;}


 

原创粉丝点击