为什么后三个点过了,前两个T?

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

sindorei @ 2023-07-31 10:19:53

#include<iostream>
using namespace std;
int A[5000005];

void half_sort(int start,int end,int need)
{
    if (start + 1 >= end)
    {
        if (A[start] > A[end]) swap(A[start] , A[end]);
        return;
    }
    int mid = A[(start + end)/2];
    int tip1 = start , tip2 = end;
    while (1)
    {
        while (A[tip1] < mid) ++tip1;
        while (A[tip2] > mid) --tip2;
        if (tip1 < tip2) swap(A[tip1] , A[tip2]);
        else
        {
            break;
        }
    }
    if (need > tip2)
    {
        half_sort(tip1 , end , need);
    }
    else
    {
        half_sort(start , tip2 , need);
    }
    return;
}

int main()
{
    int n,t;
    scanf("%d %d",&n,&t);
    for (int i=0;i<n;i++)
    {
        scanf("%d",&A[i]);
    }
    half_sort(0,n-1,t);
    printf("%d\n",A[t]);
    return 0;
}

by lucas_777 @ 2023-08-09 10:28:27

你看看我的!

#include <iostream>
using namespace std;
inline int r(){
    char ch = getchar();
    int x = 0,f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-'){
            f = -1;
        }
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
int n,k,a[5000050];
void qsort(int s,int t ,int k){
    int i = s,j = t,mid = a[(s+t) / 2];
    while(i <= j){
        while(a[i] < mid){
            i++;
        }
        while(a[j] > mid){
            j--;
        }
        if(i <= j){
            swap(a[i],a[j]);
            i++;
            j--;
        }
    }
    if(k <= j){
        qsort(s,j,k);
    }else if(k >= i){
        qsort(i,t,k);
    }else{
        cout << a[k+1];
        exit(0);
    }
}
int main(){
    n = r();
    k = r();
    for(int i = 1;i <= n;++i){
        a[i] = r();
    }
    qsort(1,n,k);
    return 0;
}

|