求助!请问这哪里错了啊!!!改写的答案,提交答案通过,但我的是三个w两个t

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

GGboy0 @ 2020-03-20 21:37:55

#include<bits/stdc++.h>
using namespace std;
int k;
void fenzhi(int data[],int l,int h){
    int low=l,high=h,mid=(l+h)/2;
    while(low<=high){
        while(data[high]>data[mid])
        high--;
        while(data[low]<data[mid])  
        low++;
        if(low<=high){
            int temp=data[low];
            data[low]=data[high];
            data[high]=temp;
            low++;high--;
        }
    }
    if(k<=high){
        fenzhi(data,l,high);
    }else if(k>=low){
        fenzhi(data,low,h);
    }else{
        cout<<data[high+1];
        return;
    }
}
int main(){
    int n;
    cin>>n>>k;
    int data[n];
    for(int i=0;i<n;i++)    cin>>data[i];
    fenzhi(data,0,n-1);
    return 0;
}

by GGboy0 @ 2020-03-20 21:39:03

答案是这个

#include <bits/stdc++.h>
using namespace std;
int a[5000005],k,n;
void mysort(int l,int r)
{
    int lmid=l,rmid=r,mid=a[(l+r)/2];//mid可以随机抽取,这里为了更养眼
    while(lmid<=rmid)
    {
        while(a[rmid]>mid)
            rmid--;
        while(a[lmid]<mid)
            lmid++;
        if(lmid<=rmid)
        {
            swap(a[lmid],a[rmid]);
            lmid++;rmid--;
        }
    }
    //这样就把这些数分成三部分(l <-> rmid <-> lmid <-> r),下面分治
    if(k<=rmid)mysort(l,rmid);
    else if(lmid<=k)mysort(lmid,r);
    else
    {
        cout<<a[rmid+1]<<"\n";
        return;
    }
}
int main()
{
    scanf("%d%d",&n,&k);
//  为显示本算法的高效性,于是不加快读了
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    mysort(0,n-1);
    return 0;
}

by GGboy0 @ 2020-03-20 21:39:19

菜鸟真心求助


by Scrutiny @ 2020-03-20 21:40:20

用C++自带sort不香吗


by Ryo_Yamada @ 2020-03-20 21:41:08

@GGboy0 暴力O2好像能过


by GGboy0 @ 2020-03-20 21:43:25

@伟大的哲学 比赛直接用sort?


by GGboy0 @ 2020-03-20 21:43:40

@breeze末影 提交上去是w


by GGboy0 @ 2020-03-20 21:44:00

@breeze末影 答案上去直接a了


by HearTheWindSing @ 2020-03-20 21:50:32

@GGboy0 比赛又不是不能用sort(因为现在比赛的题用单sort都解决不了)


by GGboy0 @ 2020-03-20 21:52:34

@wangyxhaha 回到问题上面来,帮忙看看错误


by HearTheWindSing @ 2020-03-20 21:57:23

这么大的数据,用cin能不t才怪


| 下一页