#45314: 請問這樣子也RE要怎麼解?


JengSwan (Jeng Swan)


#include <cstdio>

#include <algorithm>

using namespace std;

int a[1000000]

int main() {

    int n, k;

    scanf("%d", &n);

    for (int i = 0 ; i < n ; i ++) scanf("%d", &a[i]);;

    scanf("%d", &k);

    nth_element(a, a+n-k, a+n);

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

    return 0;

}

 

發現只要用任何方法存n個數字就記憶體超出上限。如果不存n個數字,要怎麼知道第k大?n個數字輸入完才輸入k,也不能用priority_queue。

#45315: Re: 請問這樣子也RE要怎麼解?


liaoweichen1024@gmail.com (M_SQRT)


#include

#include

using namespace std;

int a[1000000]

int main() {

    int n, k;

    scanf("%d", &n);

    for (int i = 0 ; i < n ; i ++) scanf("%d", &a[i]);;

    scanf("%d", &k);

    nth_element(a, a+n-k, a+n);

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

    return 0;

}

 

發現只要用任何方法存n個數字就記憶體超出上限。如果不存n個數字,要怎麼知道第k大?n個數字輸入完才輸入k,也不能用priority_queue。



https://hackmd.io/@becaido/HyeldiNW3

可參考 p12