CSU 1867 John and Health rate

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lizhaowei213/article/details/70193862
 

Description

The cold and flu season is here.John is worried about his cow. In order to monitor the situation of his cow,he do some inspecting everyday,and record the “health rate” for each cow.The “health rate” is a integer which can show the cow’s health condition.The higher a cow’s health rate is ,the healthier the cow is.What’s more,the doctor told John that the k-th small health rate is dangerous.Because the doctor thought there are at most k healthy cows. So John has to find out which number is the k-th small.Can you help him?

Input

Input contains multiple test cases.
The first line contains two integers n,k (1 ≤ n ≤ 1000000,1<=k<=n) — the number of cows and dangerous number. The second line contains n integers,and the i-th integer ai(-10000<=ai<=10000) is the i-th cow’s health rate

Output

Output a single line with the k-th small health rate.

Sample Input

2 1
3 2
5 2
-1 0 -2 5 3

Sample Output

2
-1

Hint

给一串数字,求第K大的,水题,粗暴的方式是用STL的sort函数,然后直接输出第K个。

但这个题用桶排序做更优,一个数字一个坑,然后从左到右扫一遍就行了。

#include <cstdio>
#include <cstring>
#define MAXN 10000
int n,k;
int numberBucket[MAXN*2+5];
int main(){
    while(scanf("%d%d",&n,&k)>0){
        memset(numberBucket,0, sizeof(numberBucket));
        while(n--){
            int tempNum;
            scanf("%d",&tempNum);
            numberBucket[tempNum+MAXN]++;
        }
        for(int i=0;i<MAXN*2+5;i++)
        {
            k-=numberBucket[i];
            if(k<=0){
                printf("%d\n",i-MAXN);
                break;
            }
        }
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/lizhaowei213/article/details/70193862
今日推荐