HLlll @ 2022-01-29 02:49:50
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
long long a[5000010];
inline int read()
{ //快速读入
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
void qsort(int begin, int end, int k)
{
int left = begin;
int right = end;
int mid = a[(begin + end) / 2];
if (begin >= end)
return;
while (left < right)
{
while (a[right] >= mid && left < right)
right--;
while (a[left] <= mid && left < right)
left++;
swap(a[left], a[right]);
}
a[left] = mid;
if (k - 1 < left)
qsort(begin, left - 1, k);
else if (k - 1 > left)
qsort(left + 1, end, k);
else
return;
}
int main()
{
int n, k;
//cin>>n>>k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
qsort(0, n - 1, k);
printf("%d", a[k]);
return 0;
}
第33行和34行,如果没有等号就只能过后面三个测试(前两个TLE),如果有等号就只能过前面两个测试(后面三个WA),这是为什么啊,求大佬解答
by strcmp @ 2022-01-29 03:25:03
这题快排就不是正解,不然怎么评黄了,数据范围
这个数据本来就很悬,再加上快排的递归常数直接卡死。
建议好好学
这道题sort+O2似乎可以过?
by JustinXiaoJunyang @ 2022-01-29 07:25:32
给一个
#include <bits/stdc++.h>
using namespace std;
int a[5000010];
int main()
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
printf("%d", a[k + 1]);
return 0;
}
还要吸氧吗……
by ThreeBlueOneBrown @ 2022-01-29 07:37:29
@JustinXiaoJunyang 一个log平常过不去吧
另外%%%
by 5k_sync_closer @ 2022-01-29 07:41:55
@JustinXiaoJunyang 的确
建议数据再开大 10 倍
by ThreeBlueOneBrown @ 2022-01-29 07:43:50
@5k_sync_closer 本地试了一下,数组元素范围
by 5k_sync_closer @ 2022-01-29 08:02:25
@gaoweisong123 题里数据范围
1≤a_i<10^9
你值域开到 sort
给你自动桶排了
by JustinXiaoJunyang @ 2022-01-29 08:50:13
呵呵过不了的
by JustinXiaoJunyang @ 2022-01-29 08:51:03
nth_element
开挂哦
by strcmp @ 2022-01-29 08:57:38
@JustinXiaoJunyang 我切这道题就是这么干的
by HLlll @ 2022-01-29 09:50:24
@wql463 O(n)算法?计数、桶排什么的吗 %%%