X_yugg123 @ 2023-07-11 09:40:41
代码本地vscode样例过了,但在这里用了O2优化还是全TLE,想不明白为什么。
代码如下:(注:quicksort是左闭右开区间,不明白为什么搞成闭区间在本地运行时会爆segamentation fault)
#include<bits/stdc++.h>
using namespace std;
const int N = 5e6+5;
int n, k, a[N];
int quicksort(int l, int r, int k)
{
int mid = a[l + (r - l) >> 1];
int i = l, j = r - 1;
while(i <= j){
while(a[i] < mid) i++;
while(a[j] > mid) j--;
if(i <= j){
swap(a[i], a[j]);
i++; j--;
}
}
if(l <= j && k <= j) return quicksort(l, j + 1, k);
if(i < r && i <= k) return quicksort(i, r, k);
return a[k];
}
int main()
{
scanf("%d %d", &n, &k);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
printf("%d", quicksort(0, n, k));
return 0;
}
by rickyxrc @ 2023-07-11 10:15:10
by Xuancheng_Mao @ 2023-08-11 09:30:35
吸氧