zoukankan      html  css  js  c++  java
  • hdu2852 KiKi's K-Number

    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
    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!

    这题能够用树状数组做,查找用到了二分,二分用在树状数组上感觉如虎添翼(笑)。

    这题查找的是比i这个数大d的位置是什么,所以仅仅要求出getsum(i)+d这个位置所相应的数是什么即可了。二分的时候要特殊推断一下。

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define maxn 100505
    int b[maxn],num[maxn];
    
    int lowbit(int x){
    	return x&(-x);
    }
    void update(int pos,int num)
    {
    	while(pos<=maxn){
    		b[pos]+=num;pos+=lowbit(pos);
    	}
    }
    
    int getsum(int pos)
    {
    	int num=0;
    	while(pos>0){
    		num+=b[pos];pos-=lowbit(pos);
    	}
    	return num;
    }
    
    int find(int l,int r,int x)
    {
    	int mid;
    	while(l<=r){
    		mid=(l+r)/2;
    		if(getsum(mid)>=x){
    			if(num[mid]==0){
    				r=mid-1;continue;
    			}
    			if(getsum(mid)-num[mid]<x){
    				return mid;
    			}
    			else r=mid-1;
    		}
    		else l=mid+1;
    	}
    }
    
    int main()
    {
    	int m,i,j,d,c,e;
    	while(scanf("%d",&m)!=EOF)
    	{
    		for(i=1;i<=maxn;i++){
    			b[i]=0;
    			num[i]=0;
    		}
    		for(i=1;i<=m;i++){
    			scanf("%d",&c);
    			if(c==0){
    				scanf("%d",&d);
    				num[d]++;
    				update(d,1);
    			}
    			else if(c==1){
    				scanf("%d",&d);
    				if(num[d]==0){
    					printf("No Elment!
    ");continue;
    				}
    				num[d]--;
    				update(d,-1);
    			}
    			else if(c==2){
    				scanf("%d%d",&d,&e);
    				if(getsum(maxn)-getsum(d)<e){
    					printf("Not Find!
    ");continue;
    				}
    				j=find(1,maxn,getsum(d)+e);
    				printf("%d
    ",j);
    			}
    		}
    	}
    	return 0;
    }


  • 相关阅读:
    软工结对作业
    软工个人博客作业-软件案例分析
    软工个人项目作业
    软工个人博客作业
    软工第一次个人作业
    2019OO第三单元作业总结
    2019OO第二单元作业总结
    提问回顾以及个人总结
    软工结对项目——图形交点PLUS
    个人博客作业-软件案例分析
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6859696.html
Copyright © 2011-2022 走看看