话r

turing_lisihan 2023-10-21 11:42:58

#include <bits/stdc++.h>

using namespace std;

const int maxn = 200000 + 5;

int n, k;
int a[maxn];

int kth(int l, int r, int k) {
	if (l == r) return a[l];
	int s = l, t = r;
	// YOUR CODE GOES HERE
	int p=a[(l+r)/2];
	swap(a[l], a[(l+r)/2]);
	while (s<t) {
		while (s<t&&a[t]>p)	t--;
		if (s<t) a[s++] = a[t];
		while (s<t&&a[s]<p) s++;
		if (s<t) a[t--]=a[s];
	}
	a[s]=p;
	if (k<=s-1) return kth(l, s-1, k);
	else if (k>=t+1) return kth(t+1, r, k);
	return a[s];
}

int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];
	cout << kth(1, n, k) << endl;

	return 0;
}