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


  • 相关阅读:
    【转】你必须知道的EF知识和经验
    【转】ASP.NET MVC中错误日志信息记录
    js获取当前iframe的ID
    【转】Code First 属性详解
    【转】MVC中code first方式开发,数据库的生成与更新(Ef6)
    【转】MVC form提交实体接收参数时空字符串值变成null
    【转】别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
    Jquery中.attr()和.data()的区别
    关于在用curl函数post网页数据时,遇上表单提交 type为submit 类型而且没有name和id时可能遇到的问题及其解决方法
    浅谈 php 采用curl 函数库获取网页 cookie 和 带着cookie去访问 网页的方法!!!!
  • 原文地址:https://www.cnblogs.com/herumw/p/9464753.html
Copyright © 2011-2022 走看看