都超时了急急急

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

telankesi @ 2022-11-27 21:22:59


#include <stdio.h>
int main() {
    int n;
    int k;
    int a[100001];
    scanf("%d %d", &n,&k);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        for (int j = 0; j <= i-1; j++) {
            if (a[i] < a[j]) {
                int t = a[j];
                a[j] = a[i];
                a[i] = t;
            }
    }
    }

    printf("%d", a[k]);

    return 0;
}

by Katz @ 2022-11-27 21:24:52

请注意数据范围


by preying @ 2022-11-27 21:33:31

这样肯定超时啊,要用分治排序或者直接sort(doge


by 凤南天 @ 2022-11-27 21:45:52

@preying 然鹅sort也是超时(悲


by xiaohaoaibiancheng66 @ 2022-11-27 21:48:56

@凤南天 万能吸氧


by chenzhiyuan0923 @ 2022-11-27 22:25:58

@xiaohaoaibiancheng66 o2过了


by preying @ 2022-11-30 16:31:08

@凤南天 那就用nth_element题目都告诉你了(doge)实在不行咱就学下分治


|