TLE求助!!!

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

dongrq_cs @ 2023-04-03 19:34:28

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

by _Haoomff_ @ 2023-04-03 19:35:16

@dongrq_cs 你想得太简单了


by _Haoomff_ @ 2023-04-03 19:36:20

要是这种做法就是一道红题了


by zjx331 @ 2023-04-03 19:39:01

用快排,sort()会超时


by zjx331 @ 2023-04-03 19:39:37

void qsort(int l,int r)
{
    if(l>r) return;
    int i=l,j=r,x=a[l];
    while(i<j)
    {
        while(i<j&&a[j]>=x) j--; a[i]=a[j];
        while(i<j&&a[i]<=x) i++; a[j]=a[i]; 
    }
    a[i]=x; 
    if(k<i) qsort(l,i-1);
    else if(k>i) qsort(i+1,r);
    else
    {
        printf("%d\n",a[i]);
        return;
    }
}

by xiaoming007 @ 2023-04-03 19:40:15

@Haoomff 谁跟你说的。。。


by xiaoming007 @ 2023-04-03 19:40:32

@dongrq_cs

这份代码开O2

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

by xiaoming007 @ 2023-04-03 19:41:15

Syx 用 n 遍寄录证明了是能卡过去的


by dongrq_cs @ 2023-04-03 19:42:10

@xiaoming007

谢!!!

请问是Syx吗?


by _Haoomff_ @ 2023-04-03 19:43:07

@xiaoming007 ???好家伙,你不给我调题跑来跟我辩论了?!


by dongrq_cs @ 2023-04-03 19:44:02

谢!!!

Syx对我的恩情,我没齿难忘!!!

记录详情


| 下一页