大佬求助!归并五个WA!!

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

GJbone @ 2024-02-20 18:45:34

#include<bits/stdc++.h>
using namespace std;
int n,k,a[5000001];

void merget(int* a,int low,int mid,int hight){
    int* b=new int[hight-low+1];
    int i = low,j=mid+1,k=0;
    while(i<=low&&j<=hight){
        if(a[i]<a[j]){
            b[k++]=a[i++];
        }
        else{
            b[k++]=a[j++];
        }
    }
    while(i<=mid){
        b[k++]=a[i++];
    }
    while(j<=hight){
        b[k++]=a[j++];
    }

    k=0;
    for(i=low;i<=hight;i++){
        a[i]=b[k++];
    }
    delete []b;   
}

void mergetsort(int* a,int low,int hight){
    if(low<hight){
        int mid=(low+hight)/2;
        mergetsort(a,low,mid);
        mergetsort(a,mid+1,hight);
        merget(a,low,mid,hight);
    }
}

int main(){
    int i;
    scanf("%d%d",&n,&k);
    for(i=0;i<=n;i++){
    scanf("%d",&a[i]);
    }   
    mergetsort(a,0,n-1);
    printf("%d",a[k]);   
    return 0;
}

by Rockey0515 @ 2024-02-20 18:52:13

while(i<=low&&j<=hight)
i<=low吧

by Rockey0515 @ 2024-02-20 18:53:37

@GJbone 应该是 mid


by GJbone @ 2024-02-20 19:05:11

@Rockey0515 啊这...我还看了好久,不好意思了


by GJbone @ 2024-02-20 19:06:05

@Rockey0515 这个还能优化嘛?最后一个超时了


|