COJ-1867-John and Health rate

来源:互联网 发布:nat123端口映 编辑:程序博客网 时间:2024/05/17 08:45

1867: John and Health rate

Submit Page Summary Time Limit: 2 Sec Memory Limit: 128 Mb Submitted: 367 Solved: 85
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
Source
中南大学第十一届大学生程序设计竞赛网络预选赛

题目大意:求序列中的第k小数。
解题思路:利用快排的划分思想,减治思想。

#include<iostream>#include<cstring>#include<cmath>#include<ctime>#include<cstdlib>#include<algorithm>#include<iomanip>#include<fstream>#include<map>using namespace std;const int MAXN=1e6+5;int a[MAXN];int Partition(int L,int R){    int i,j,x,tmp;    i=L;    j=R+1;    x=a[L];    while(true)    {        while(a[++i]<x);        while(a[--j]>x);        if(i>=j) break;        tmp=a[i];        a[i]=a[j];        a[j]=tmp;    }    a[L]=a[j];    a[j]=x;    return j;}int RandomPar(int L,int R){    int i=L+rand()%(R-L+1);    int tmp=a[i];    a[i]=a[L];    a[L]=tmp;    return Partition(L,R);}int Find(int L,int R,int k){    int i,j;    if(L==R) return a[L];    i=RandomPar(L,R);    //i=Partition(L,R);    j=i-L+1;    if(k==j) return a[i];    if(k<j) return Find(L,i-1,k);    else return Find(i+1,R,k-j);}int main(){    int n,k;    while(scanf("%d%d",&n,&k)!=EOF)    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }//        for(int i=1;i<=n;i++)//        {//            cout<<a[i]<<" ";//        }//        cout<<endl;        printf("%d\n",Find(1,n,k));    }}
0 0
原创粉丝点击