Pagini recente » Cod sursa (job #1072181) | Cod sursa (job #388525) | Cod sursa (job #935548) | Cod sursa (job #554120) | Cod sursa (job #2983247)
#include<bits/stdc++.h>
using namespace std;
unsigned int A[3000005];
ifstream in("sdo.in");
ofstream out("sdo.out");
int part(int l,int h){
int pivot=A[h];
int i=l;
for(int j=l;j<h;j++)
if(A[j]<=pivot)
swap(A[i++],A[j]);
swap(A[i],A[h]);
swap(A[i],A[h]);
return i;
}
int r_part(int l, int h){
srand((unsigned)time(NULL));
int i=l+rand()%(h-l+1);
swap(A[i],A[h]);
return part(l,h);
}
void r_select(int l,int h,int i){
if(l>=h)
return ;
int pivot=r_part(l,h);
if(pivot==i)
return;
if(i<pivot)
r_select(l,pivot-1,i);
if(i>pivot)
r_select(pivot+1,h,i);
}
int main(){
int n,f;
in>>n>>f;
for(int i=1;i<=n;i++)
in>>A[i];
r_select(1,n,f);
out<<A[f];
}
/*
7 1
477151 3 4613 65 4 12 65
*/