P1923 后两个点超时 请帮我看看代码的问题

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

XiaoYuyu @ 2023-10-31 11:56:10

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int n, ans,k;
int partition(int a[], int low, int high)
{
    int pivot = a[low];
    while (low < high)
    {
        while (low < high && a[high] >= pivot)
            high--;
        a[low] = a[high];
        while (low < high && a[low] <= pivot)
            low++;
        a[high] = a[low];
    }
    a[low] = pivot;
    return low;

}
int quicksort(int a[], int low, int high)
{
    if (low < high)
    {
        int pivot = partition(a, low, high);
        if (k == pivot)
        {
            return pivot;
        }
        if (k <= pivot)
        quicksort(a, low, pivot - 1);
        if(k>= pivot)
        quicksort(a, pivot + 1, high);
    }

}

int main()
{
    int  a[5000005]={0};
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    quicksort(a, 0, n-1);
    cout << a[k] << endl;

}

请求帮我debug


by zhangbo1000 @ 2023-10-31 12:39:41

cout cin 效率低下,可以在main最前面加上

iostream::sync_with_stdio(0);
cin.tie(0);

并用'\n'代替endl


|