洛谷 P2032 扫描

传送门

思路

单调队列例题,有多倍经验哦~~

多倍经验传送门:

P1886 滑动窗口

P1440 求m区间内的最小值

代码

//知识点:单调队列 
/*
By:Loceaner
*/
#include 
#include 
#include 
using namespace std;

inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
    return x * f;
}

const int N = 2e6 + 11;

int n, k, q[N], a[N];

void getmax() {
    int head = 0, tail = 0;
    for(int i = 1; i < k; i++) {
        while(head <= tail && a[q[tail]] <= a[i]) tail--;
        q[++tail] = i;
    }
    for(int i = k; i <= n; i++) {
        while(head <= tail && a[q[tail]] <= a[i]) tail--;
        q[++tail] = i;
        while(q[head] <= i - k) head++;
        cout << a[q[head]] << '\n';
    }
}

int main() {
    n = read(), k = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    getmax();
    return 0;
}

你可能感兴趣的:(洛谷 P2032 扫描)