一.题目链接:

POJ-2823

二.题目大意:

有 n 个数,用一个窗口去扫描,窗口大小为 k.

求每次扫描得到的最小值与最大值.

三.分析:

单调队列 O(N)扫描即可.

又加深了自己对单调队列的理解.

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long
#define ull unsigned long long
using namespace std;

const int M = (int)1e6;
const int mod = 99991;
const int inf = 0x3f3f3f3f;

int a[M + 5];
int q[M + 5];

/**
8 3
1 3 -1 -3 5 3 6 7
**/

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    int l = 1, r = 1;
    q[1] = 0, a[0] = inf;
    for(int i = 1; i <= n + 1; ++i)
    {
        while(l <= r && q[l] < i - k)   l++;
        if(i > k)
            printf("%d%c", a[q[l]], i == n + 1 ? '\n' : ' ');
        while(l <= r && a[i] <= a[q[r]])    r--;
        q[++r] = i;
    }
    l = 1, r = 1;
    q[1] = 0, a[0] = -inf;
    for(int i = 1; i <= n + 1; ++i)
    {
        while(l <= r && q[l] < i - k)   l++;
        if(i > k)
            printf("%d%c", a[q[l]], i == n + 1 ? '\n' : ' ');
        while(l <= r && a[i] >= a[q[r]])    r--;
        q[++r] = i;
    }
    return 0;
}