letusgo @ 2023-11-15 21:50:06
#include<bits/stdc++.h>
using namespace std;
int a[5000005],n,k;
void quick_sort(int l, int r){
int mid = a[(l+r)/2], i = l, j = r;
while(i <= j){ //i < j时结束循环
while(a[i] < mid) i++;
while(a[j] > mid) j--;
swap(a[i],a[j]);
i++,j--;
}
if(k <= j)quick_sort(l,j);
if(k >= i)quick_sort(i,r);
if(k < i && k > j){
cout << a[i-1];
exit(0);
}
}
int main(){
cin >> n >> k;
k += 1;
for(int i = 1; i <= n; i++) cin >> a[i];
quick_sort(1,n);
//for(int i = 1; i <= n; i++) cout << a[i];
return 0;
}
by _Supernova @ 2023-11-15 22:07:10
@letusgo 用 sort 会更好,没必要用这个复杂的。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e6 + 5;
int n, k, a[maxn];
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
sort(a, a + n);
printf("%d", a[k]);
return 0;
}
by letusgo @ 2023-11-15 22:34:04
@XX_Traveller_XX sort好像会超时
by _Supernova @ 2023-11-16 20:45:39
@letusgo 开