#include<cstdio>
int n, k;
long long a[5000001];
long long quickfind(int low, int high, int number)
{
if (low == high)
return a[low];
int i = low, j = high + 1;
long long key = a[low], temp;
for (;;)
{
for (; a[++i] < key;)
if (i == high)
break;
for (; a[--j] > key;)
if (j == low)
break;
if (i >= j)
break;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
temp = a[j];
a[j] = a[low];
a[low] = temp;
if (j == k)
return a[j];
else if (k > j)
return quickfind(j + 1, high, k - j);
else if (k < j)
return quickfind(low, j - 1, k);
}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
printf("%lld", quickfind(0, n - 1, k));
return 0;
}
洛谷P1923 【深基9.例4】求第 k 小的数进阶解法
最新推荐文章于 2024-07-02 09:05:18 发布