求助

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

PRAGMATISM @ 2020-03-19 13:57:05

这一题cin/cout超时吗?


by tangrunxi @ 2020-03-19 13:57:20

试试不就知道了


by 江户川·萝卜 @ 2020-03-19 13:57:36

自 己 试 试


by njcx260 @ 2020-03-19 13:57:37

估计会


by DeepSkyBlue__ @ 2020-03-19 13:57:39

最好别用(


by njcx260 @ 2020-03-19 13:57:54

看ac率


by PRAGMATISM @ 2020-03-19 13:58:25

我试了,用的sort,后两个点TLE


by 江户川·萝卜 @ 2020-03-19 13:58:50

如果想偷懒可以加个std::ios::sync_with_stdio(false)
貌似速度会更快


by ChthollyForever @ 2020-03-19 14:05:33

@2020_

上快读,940+ ms


by Aw顿顿 @ 2020-03-19 14:13:13

常数优化还能再优化吗qwq

#include<bits/stdc++.h>
using namespace std;
long long n,k,z,a[10000001];
inline long long read(){
    long long s=0,f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!=EOF){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*f;
}
void q_sort(int l,int r){
    int mid=a[(l+r)/2];
    int i=l,j=r;
    do{
        while(a[i]<mid)i++;
        while(a[j]>mid)j--;
        if(i<=j){
            swap(a[i],a[j]);
            i++;
            j--;
        }
    }while(i<=j);
    if(l<j)q_sort(l,j);
    if(i<r)q_sort(i,r);
}
int main(){
    n=read();
    k=read();
    for(int i=0;i<n;i++){
        a[i]=read();
    }
    q_sort(0,n-1);
    printf("%lld\n",a[k]);
    return 0;
}

by PRAGMATISM @ 2020-03-19 16:18:14

谢谢


|