RMQ70分,3个点MLE求助

P1886 滑动窗口 /【模板】单调队列

czcyf @ 2022-07-21 19:29:45

rt,单调队列已经AC了,就是想用RMQ试试,然后就逝世了……

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int maxn[N][25],minn[N][25];
int main()
{
    int n,k;
    cin >> n >> k;
    for (int i=1;i<=n;i++)
    {
        cin >> maxn[i][0];
        minn[i][0]=maxn[i][0];
    }
    for (int j=1;(1<<j)<=n;j++)
    {
        for (int i=1;i+(1<<j-1)<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<j-1)][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<j-1)][j-1]);
        }
    }
    for (int i=1;i<=n-k+1;i++)
    {
        int l=i,r=i+k-1;
        int kk=log2(k);
        cout << min(minn[l][kk],minn[r-(1<<kk)+1][kk]) << " ";
    }
    cout << endl;
    for (int i=1;i<=n-k+1;i++)
    {
        int l=i,r=i+k-1;
        int kk=log2(k);
        cout << max(maxn[l][kk],maxn[r-(1<<kk)+1][kk]) << " ";
    }
    cout << endl;
    return 0;
}

by dxy2020 @ 2022-07-21 19:32:04

用一个数组,算两次。

少一倍空间。


|