AcWing 786. 第k个数
原题链接
简单
作者:
沙悟净
,
2021-05-16 13:56:02
,
所有人可见
,
阅读 3
#include <iostream>
using namespace std;
const int SIZE = 1e6 + 10;
int array[SIZE];
int partition(int* q, int n, int pivot) {
int x = q[pivot], i = -1, j = n;
while (i < j) {
do i++; while (q[i] < x);
do j--; while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
return q[pivot];
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &array[i]);
while (array[k - 1] != partition(array, n, k - 1));
std::cout << array[k - 1];
return 0;
}