xffdeg @ 2024-08-27 20:04:49
#include <iostream>
using namespace std;
int ans = 0 , k;
void findkth(int a[] , int l , int r){
if(l == r){
ans = a[l];
return;
}
int i = l , j = r , flag = a[(l + r) / 2] , tmp;
do{
while(a[i] < flag) i++;
while(a[j] > flag) j--;
if(i <= j){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
}while(i <= j);
if(k <= j)findkth(a, l, j);
else if(k <= i)findkth(a, i, r);
else findkth(a , j + 1 , i - 1);
}
int main (){
int n;
cin >> n >> k;
int a[n];
for(int i = 0;i < n;i++)
cin >> a[i];
findkth(a, 0, n - 1);
cout << ans << endl;
return 0;
}
如果能优化的话送关注
by dream_dad @ 2024-08-27 20:06:18
论快读的重要性快读
by xffdeg @ 2024-08-27 20:08:03
@dream_dad 怎么改呢
by chenhaihang @ 2024-08-27 20:08:13
@xffdeg 你这个要不换个思路我给你说个nb的函数这种题可以秒
by chenhaihang @ 2024-08-27 20:08:46
@xffdeg 你这个都n方了
by Yxy7952 @ 2024-08-27 20:10:19
@xffdeg
优化是优化不了一点,只能换算法
by dream_dad @ 2024-08-27 20:10:37
STL是个好东西
by xffdeg @ 2024-08-27 20:11:03
@yixingyou OK
by Yxy7952 @ 2024-08-27 20:11:04
@xffdeg
不知道楼主认不认得sort?
by xffdeg @ 2024-08-27 20:12:10
@yixingyou 知道了,谢谢啦
by dream_dad @ 2024-08-27 20:13:06
nth_element函数也行