1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include<bits/stdc++.h> using namespace std;
const int N = 100000+10;
int n, k; int q[N];
int quick_sort(int q[], int l, int r, int k) { if (l >= r) return q[l]; int i = l - 1, j = r + 1, x = q[l + r >> 1]; while (i < j) { do i++ ; while (q[i] < x); do j-- ; while (q[j] > x); if (i < j) swap(q[i], q[j]); } int sl = j - l + 1; if (k <= sl) return quick_sort(q, l, j, k); else return quick_sort(q, j + 1, r, k-sl); }
int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> q[i]; cout << quick_sort(q, 0, n-1, k) << endl; return 0; }
|