Pagini recente » Cod sursa (job #886312) | Cod sursa (job #2584032) | Cod sursa (job #189460) | Cod sursa (job #349945) | Cod sursa (job #1265961)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#define INF (1<<30)
#define mod 666013
using namespace std;
int n, k, i, a[3000005];
void chg(int x, int y)
{
int aux=a[x];
a[x]=a[y];
a[y]=aux;
}
int piv(int st, int dr)
{
int x=st-1, y=dr+1;
int rnd=rand()%(dr-st+1);
int pv=a[st+rnd];
while(1)
{
x++;
while(a[x]<pv)
x++;
y--;
while(a[y]>pv)
y--;
if(x<y)
chg(x, y);
else
return y;
}
return 0;
}
void sdo(int st, int dr, int k)
{
if(st>=dr) return;
int w=piv(st, dr);
int q=w-st+1;
if(q>=k)
sdo(st, w, k);
else
sdo(w+1, dr, k-q);
}
int main()
{
freopen("sdo.in", "r", stdin);
freopen("sdo.out", "w", stdout);
srand(time(0));
scanf("%d%d", &n, &k);
for(i=1;i<=n;i++)
scanf("%d", &a[i]);
sdo(1, n, k);
printf("%d", a[k]);
return 0;
}