求助大佬,60分最后两个测试点RE

P1923 【深基9.例4】求第 k 小的数

_Maverick_ @ 2023-09-30 19:11:05

#include<bits/stdc++.h>
using namespace std;
long long x[4000000],k;
long long n;
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n);
    cout<<x[k];
}

by 137QWQ @ 2023-09-30 19:14:42

n<5000000

数组开小了


by 137QWQ @ 2023-09-30 19:16:15

快排nlogn过不去 得二分O(n)


by WYZ20030051 @ 2023-09-30 19:31:15

@137QWQ 然而快排能过


by WYZ20030051 @ 2023-09-30 19:31:56

@137QWQ 提交记录


by 137QWQ @ 2023-09-30 20:30:37

@WYZ20030051


by _Maverick_ @ 2023-10-01 07:40:39

#include<bits/stdc++.h>
using namespace std;
long long x[5000000+10],k;
long long n;
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n);
    cout<<x[k];
}

@137QWQ @WYZ20030051


by _Maverick_ @ 2023-10-01 07:42:12

@137QWQ @WYZ20030051 这样,只得60分,最后两个测试点TLE了


by WYZ20030051 @ 2023-10-01 11:25:47

@myh00198100 我用了快读和printf输出,还开了O2,用排序做需要卡卡常,常数大是过不去的


by WYZ20030051 @ 2023-10-01 11:26:37

@myh00198100

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<cassert>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<cstdlib>
using namespace std;
#define ll long long
#define ull unsigned long long
int read()
{
    int now=0,nev=1;
    char c=getchar();
    while(c<'0' || c>'9')
    {
        if(c=='-')
            nev=-1;
        c=getchar();
    }
    while(c>='0' && c<='9')
    {
        now=(now<<1)+(now<<3)+(c&15);
        c=getchar();
    }
    return now*nev;
}
const int MAXN=5e6+10;
int n,k;
int a[MAXN];
int main()
{
    n=read(),k=read();
    for(int i=0;i<n;i++)
        a[i]=read();
    sort(a,a+n);
    printf("%d",a[k]);
    return 0;
}

by _Maverick_ @ 2023-10-01 13:19:39

@WYZ20030051 谢谢


|