HDU2852-KiKi's K-Number

来源:互联网 发布:pca算法matlab代码 编辑:程序博客网 时间:2024/06/05 18:50

KiKi's K-Number

                                                                             Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                                        Total Submission(s): 3717    Accepted Submission(s): 1654


Problem Description
For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. Now Kiki meets a very similar problem, kiki wants to design a container, the container is to support the three operations.

Push: Push a given element e to container

Pop: Pop element of a given e from container

Query: Given two elements a and k, query the kth larger number which greater than a in container;

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

Input
Input some groups of test data ,each test data the first number is an integer m (1 <= m <100000), means that the number of operation to do. The next m lines, each line will be an integer p at the beginning, p which has three values:
If p is 0, then there will be an integer e (0 <e <100000), means press element e into Container.

If p is 1, then there will be an integer e (0 <e <100000), indicated that delete the element e from the container  

If p is 2, then there will be two integers a and k (0 <a <100000, 0 <k <10000),means the inquiries, the element is greater than a, 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
50 51 20 62 3 22 8 170 20 20 42 1 12 1 22 1 32 1 4
 

Sample Output
No Elment!6Not Find!224Not Find!
 

Source
2009 Multi-University Training Contest 4 - Host by HDU

       题意:给定一个容器,里面存放各种数值,规定三个操作,一个是在容器中增加一个数值,一个是在容器中删掉一个数值,一个是询问容器中比a大的数中第k大的数,将其输出。如果在删除过程中没有这个数,则输出"No Elment!",如果容器中没有比a大的第k个数,则输出"Not Find!"。

     解题思路:采用一个标记数组来标记某个数值在容器中是否有。对于询问容器中比a大的第k个数,采用二分法,对a后面比它大的数进行二分,二分时必须是查看某点至a点有多少个比它大的,用sum(mid) - Sum(a)就可以得到,只要二分mid就可以了。


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;const int MAX=100005;int num[MAX];int lowbit(int x){    return x&(-x);}void add(int x ,int val){    while(x<MAX)    {        num[x]+=val;        x+=lowbit(x);    }}int sum(int x){    int cnt=0;    while(x>0)    {        cnt+=num[x];        x-=lowbit(x);    }    return cnt;}int Find(int pos,int k){    int p=sum(pos);    int l=pos+1,r=MAX,mid;    int ans=MAX,cnt;    while(l<=r)    {        mid=(l+r)/2;        cnt=sum(mid)-p;        if(cnt>=k)        {            r=mid-1;            ans=min(ans,mid);        }        else l=mid+1;    }    return ans;}int main( ){    int n;    int c,x,k;    while(~scanf("%d",&n))    {        memset(num,0,sizeof(num));        while(n--)        {            scanf("%d",&c);            if(c==0)            {                scanf("%d",&x);                add(x,1);            }            else if(c==1)            {                scanf("%d",&x);                if(sum(x)-sum(x-1)==0) printf("No Elment!\n");                else add(x,-1);            }            else if(c==2)            {                scanf("%d%d",&x,&k);                int cnt=Find(x,k);                if(cnt==MAX) printf("Not Find!\n");                else printf("%d\n",cnt);            }        }    }    return 0;}

0 0