hdu2852 KiKi's K-Number 树状数组+二分查找求第k大的数

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

5
0 5
1 2
0 6
2 3 2
2 8 1
7
0 2
0 2
0 4
2 1 1
2 1 2
2 1 3
2 1 4

Sample Output

No Elment!
6
Not Find!
2
2
4
Not Find!

树状数组用来标记每个数个数

查询正好是查询这个数之前的个数 也就是第几大

然后减去query(a)的如果正好是k 说明就是比a大的第k大的数字

查找运用的二分查找

巧妙运用了树状数组 标记的过程类似于查询逆序数对的过程 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef __int64 LL;
using namespace std;

const int MAX=100000+10;
int c[MAX];
int num[MAX];
int lowbit(int x){
	return x&(-x);
}
void Update(int x,int d){
	while(x<=MAX){
		c[x]+=d;
		x+=lowbit(x);
	}
}
int Query(int x){
	int sum=0;
	while(x>0){
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
}

int main(){
	int m;
	while(scanf("%d",&m)!=EOF){
	    int f,coun=0;
	    memset(c,0,sizeof(c));
	    memset(num,0,sizeof(num));
        while(m--){
            scanf("%d",&f);
            if(f==0){
                int x;
                scanf("%d",&x);
                Update(x,1);num[x]++;
                coun++;
            }
            else if(f==1){
                int x;
                scanf("%d",&x);
                if(num[x]==0){printf("No Elment!\n");}
                else {
                Update(x,-1);
                num[x]--;coun--;}
            }
            else {
                int a,k;
                scanf("%d%d",&a,&k);
                int res=Query(a);
                if(res+k>coun)printf("Not Find!\n");
                else {
                    int l=a,r=MAX,ans;
                    while(l<=r){
                        //cout<<233<<endl;
                        int mid=(l+r)/2;
                        if(Query(mid)-res>=k)r=mid-1,ans=mid;
                        else l=mid+1;
                    }
                    printf("%d\n",ans);
                }
            }
        }
	}
	return 0;
}

 

全部评论

相关推荐

2025-12-28 22:19
门头沟学院 Java
不敢追165女神:简历写得毫无特点,你说你要是大二或者大三找寒假实习到暑期实习这段时间,你的简历还能约到面试。但是你是研究生哥,面试官不会因为你是研究生而降低要求,反而会觉得你是研究生才学了这么一点?为什么我不找个同阶段的本科生?
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务