2017-07-22 07:15:34 +03:00
|
|
|
#include <stdio.h>
|
2020-04-08 16:41:12 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
void swap(int *a, int *b)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
t = *a;
|
|
|
|
*a = *b;
|
|
|
|
*b = t;
|
|
|
|
}
|
|
|
|
int part(int a[], int l, int r, int n, int pivot, int pindex)
|
|
|
|
{
|
|
|
|
int p1 = l, p2 = r;
|
|
|
|
while (p2 > p1)
|
2017-07-22 07:15:34 +03:00
|
|
|
{
|
2020-04-08 16:41:12 +03:00
|
|
|
if (a[p1] > pivot && a[p2] < pivot)
|
|
|
|
{
|
|
|
|
swap(&a[p1], &a[p2]);
|
|
|
|
}
|
2017-07-22 07:15:34 +03:00
|
|
|
else
|
|
|
|
{
|
2020-04-08 16:41:12 +03:00
|
|
|
if (a[p1] <= pivot)
|
|
|
|
{
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
if (a[p2] >= pivot)
|
|
|
|
{
|
|
|
|
p2--;
|
|
|
|
}
|
2017-07-22 07:15:34 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 16:41:12 +03:00
|
|
|
swap(&a[pindex], &a[p2]);
|
2017-07-22 07:15:34 +03:00
|
|
|
return p2;
|
|
|
|
}
|
2020-04-08 16:41:12 +03:00
|
|
|
int rselect(int a[], int l, int r, int n, int o)
|
2017-07-22 07:15:34 +03:00
|
|
|
{
|
2020-04-08 16:41:12 +03:00
|
|
|
int pivot, pindex, pactual;
|
|
|
|
if (r > l)
|
|
|
|
{
|
|
|
|
pindex = rand() % (r - l + 1);
|
|
|
|
pivot = a[pindex];
|
|
|
|
pactual = part(a, l, r, n, pivot, pindex);
|
|
|
|
|
|
|
|
if (pactual == o)
|
|
|
|
{
|
|
|
|
return a[pactual];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o < pactual)
|
|
|
|
{
|
|
|
|
rselect(a, l, pactual - 1, n, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o > pactual)
|
|
|
|
{
|
|
|
|
rselect(a, pactual + 1, r, n, o - pactual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r == l)
|
2017-07-22 07:15:34 +03:00
|
|
|
{
|
2020-04-08 16:41:12 +03:00
|
|
|
return a[l];
|
2017-07-22 07:15:34 +03:00
|
|
|
}
|
2020-04-08 16:41:12 +03:00
|
|
|
return -1;
|
2017-07-22 07:15:34 +03:00
|
|
|
}
|
|
|
|
int main()
|
2020-04-08 16:41:12 +03:00
|
|
|
{
|
|
|
|
srand(time(NULL));
|
|
|
|
int n, o, i, *a;
|
|
|
|
scanf("%d %d", &n, &o);
|
|
|
|
a = (int *)malloc(n * sizeof(int));
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
scanf("%d", a + i);
|
|
|
|
}
|
|
|
|
printf("\n\n%d", rselect(a, 0, n - 1, n, o));
|
2017-07-22 07:15:34 +03:00
|
|
|
return 0;
|
|
|
|
}
|