Pagini recente » Cod sursa (job #3121084) | Cod sursa (job #444651) | Cod sursa (job #2749634) | Cod sursa (job #1378975) | Cod sursa (job #373454)
Cod sursa(job #373454)
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
ifstream fi("sdo.in");
ofstream fo("sdo.out");
int N,A[3000010],K;
inline int poz(int* A,int p,int u){
int pivot=A[p+rand()%(u-p+1)];
int start=p,end=u;
while(1){
while (A[start]<pivot) ++start;
while (A[end]>pivot) --end;
if (start<end) swap(A[start],A[end]);
else return start;
}
}
void kth_elem(int *A,int p,int u,int k){
if (p==u) return ;
int m=poz(A,p,u);
int x=m-p+1;
if (x>k) kth_elem(A,p,m-1,k); else if (x<k)
kth_elem(A,m+1,u,k-x);
}
int main(){
srand(time(NULL));
fi>>N>>K;
for (int i=1;i<=N;++i) fi>>A[i];
kth_elem(A,1,N,K);
fo<<A[K];
fi.close();fo.close();
return 0;
}