各位大佬帮看看,为啥后两个显示的是re

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

sixgoder @ 2023-08-17 14:49:23

#include<iostream>
using namespace std;
int n;//n个数 
long long data[5000031];
long long  tmp[5000031];
int nums = 1;
// 快读 
long long read(){
    long long x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-'){
            f = -1;
        }
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return x*f;
}
void hebing(int start,int mid,int end ){
    int POS1 = start;
    int POS2 = mid+1;
    int old = nums;

    while(POS1<=mid&&POS2<=end){
        if(data[POS1]>=data[POS2]){
            tmp[nums++] = data[POS2++];
        }else{
            tmp[nums++] = data[POS1++];
        }
    }
    while(POS2<=end){
        tmp[nums++] = data[POS2++];
    }
    while(POS1<=mid){
        tmp[nums++] = data[POS1++];
    }
    copy(tmp+old,tmp+nums,data+start);
    return ;
} 
void huafen(int start,int end){
    if(start<end&&start!=0&&end!=0){
        long long mid = (start+end)/2;// 中间值的下标
        huafen(start,mid);
        huafen(mid+1,end);
        hebing(start,mid,end); // 合并操作 
    }
    return;
}
int main()
{
    long long k;
    cin>>n>>k;
    for(int  i=1;i<=n;i++){
        data[i] = read();
    }
    huafen(1,n);
    cout<<data[k+1];

    return 0;
}

by OldDriverTree @ 2023-08-17 15:00:20

@sixgoder 你交了吗?


by I_like_play_pubg @ 2023-08-17 15:03:21

#include<bits/stdc++.h>
using namespace std;
long long n,k,a[5000010];
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    nth_element(a+1,a+k+1,a+n+1); 
    printf("%d",a[k+1]);
}

这样写不就可以了吗

为什么偏要听题目上的不用nth_element


by sixgoder @ 2023-08-17 15:38:55

@wqc20111126 我主要是想用分治法解决,用nth_element就没有意义了


by sixgoder @ 2023-08-17 15:39:59

@OldDriverTree 提交了


|