题意:nth_element
题解:stl中nth_element函数或者(伪)快排?
nth_element(array, array+k, array+len);
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 10000005
#define ll long long
#define inf 0x7fffffff
#define OO __attribute__((optimize("-O2")))
int n,m;
int a[N];
int read(){
int x=0;bool f=0;char ch;
for(ch=getchar();ch<'0'||ch>'9';ch=getchar())f=ch=='-';
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+(ch^48);if(f)x=-x;return x;
}
void q_Sort(int L,int R){
if(L>=R) return;
int mid=L+(R-L)/3;
swap(a[L],a[mid]);
int l=L,r=R;int key=a[L];
while(l<r){
while(l<r&&a[r]>key) r--;
a[l]=a[r];
while(l<r&&a[l]<=key) l++;
a[r]=a[l];
}
a[l]=key;
if(m<l) q_Sort(L,l-1);
if(m>r) q_Sort(r+1,R);
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=n;i;i--)
a[i]=read();
//for(int i=1;i<=n;i++) swap(a[rand()%n+1],a[rand()%n+1]);
q_Sort(1,n);
printf("%d\n",a[m]);
}
return 0;
}