我发现直接手敲快排TLE后,加了一个判断在R-L<=32时使用插入排序就AC了

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

asd890123 @ 2024-05-13 18:50:26

void insert_sort(int v[],int l,int r){

    int i,j,temp;

    for (i = l;i <= r;i++){

        temp = v[i];
        for (j = i - 1;j >= l && temp < v[j];j--) v[j + 1] = v[j];
        v[j + 1] = temp;

    }
}

void quik_sort(int v[],int l,int r){

    std::stack <std::pair <int,int> > s;

    s.push({l,r});
    while (!s.empty()){

        int L = s.top().first,R = s.top().second;
        int i = L,j = R,flag = v[rand() % (R - L + 1) + L];

        s.pop();
        if (R - L <= 32) insert_sort(v,L,R);
        do{

            while (v[i] < flag) i++;
            while (v[j] > flag) j--;
            if (i <= j)
                std::swap(v[i],v[j]),i++,j--;

        }while (i <= j);
        if (L < j) s.push({L,j});
        if (i < R) s.push({i,R});

    }

}

by asd890123 @ 2024-05-13 18:50:40

This is why


by ZMQ_Ink6556 @ 2024-05-13 18:57:46

IDK,但是用 sort() 可能更省事


by _Kenma_ @ 2024-05-13 19:48:53

nlogn过5e6?T的原因是复杂度不正确,而你的玄学优化减少了递归层数,优化了常数,所以卡过了,用sort绝对是过不去的


by _Kenma_ @ 2024-05-13 19:51:56

@zhangmoqing 你是不是没看题就开始打字了(逃)


by _Kenma_ @ 2024-05-13 19:52:19

@asd890123


by SunsetLake @ 2024-05-13 19:53:41

@xutianze 你说的对但是,sort 能过


by _Kenma_ @ 2024-05-13 19:57:13

啊?我试试


by _Kenma_ @ 2024-05-13 20:00:38

是真的,就离谱,不愧是少爷机@SunserLake


by _Kenma_ @ 2024-05-13 20:03:06

(误)@SunsetLake


by _Kenma_ @ 2024-05-13 20:06:29

不是,sort+O2比某些题解正解快?


|