后两个超时求助

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

yeanna @ 2022-08-10 21:33:18

照时空小涵的题解自己敲的,为什么会超时TAT

#include<iostream>
using namespace std;
int a[5000000];
int quickSort(int low, int high)
{
    int mid = a[low];
    while (low < high)
    {
        while (a[high] >= mid && low < high)high--;
        if(low<high)a[low++] = a[high];
        while (a[low] <= mid&&low < high)low++;
        if(low<high)a[high--] = a[low];
    }
    a[low] = mid;
    return low;
}
void find(int low, int high, int k)
{
    int index = quickSort(low, high);
    if (index == k)
    {
        cout<<a[index];
    }
    else if (index > k)
    {
        find(low, index - 1, k);
    }
    else
        find(index + 1, high, k);
}
int main()
{
    int n, k;
    cin >> n >> k;
    for (int i = 0;i < n;i++)
    {
        cin >> a[i];
    }
    find(0, n-1, k);
    return 0;
}

by Nicrobot @ 2022-08-11 16:51:51


|