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?
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.
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
- hdu2852 KiKi's K-Number
- hdu2852 KiKi's K-Number
- HDU2852-KiKi's K-Number
- hdu2852——KiKi's K-Number
- hdu2852 KiKi's K-Number 线段树
- HDU2852 KiKi's K-Number 树状数组+二分
- HDU2852 KiKi's K-Number 线段树单点更新
- HDU2852 KiKi's K-Number(二分+树状数组)
- HDU2852 KiKi's K-Number (主席树)
- hdu2852--KiKi's K-Number(线段树,求第k个数)
- hdu2852 KiKi's K-Number 树状数组求第k大数
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
- HDU2852 KiKi's K-Number (线段树求数列K大)
- HDU2852-KiKi's K-Number(树状数组+二分|权值线段树)
- KiKi's K-Number
- HDU 2852 KiKi's K-Number
- hdu 2852 KiKi's K-Number
- hdu 2852 KiKi's K-Number
- universal-image-loader如何设置本地图片
- 永远的夙愿
- studio android.useDeprecatedNdk=true解决ndk提示版本低不能自动编译jni
- POJ 1011 Sticks DFS+很多剪枝 *
- Android实战:手把手实现“捧腹网”APP(一)-----捧腹网网页分析、数据获取
- HDU2852-KiKi's K-Number
- 安卓自动化测试工具robotium android的简介及安装
- Could not generate DH keypair 解决方案
- 马哥视频课程学习笔记 gawk
- linux运维-计划任务
- 科大讯飞面试经验
- [2016ICPC 大连网络预选赛] HDU5876 图论
- EL表达式取值
- linux运维-log日志管理