Sliding Window POJ - 2823

Posted zgglj-com

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sliding Window POJ - 2823相关的知识,希望对你有一定的参考价值。

技术分享图片

题解:从前往后,维护一个从栈底到栈顶递增的栈,那么每个区间的最小值就是栈底。从后往前,维护一个从栈顶到栈底递增的栈,那么每个区间的最大值就是栈底。

//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<stack>
#define ll long long
#define P pair<int, int>
#define PP pair<int,pair<int, int>>
#define pb push_back
#define pp pop_back
#define lson root << 1
#define INF (int)2e9 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

const int N = 1e6 + 10;

int n, k;
int a[N], b[N], c[N], Max[N], Min[N];

void solve() {
    int l = 0, r = 0;
    for(int i = n;  i >= 1; i--) {
        while(r > l && b[l] > i + k - 1) l++;
        while(r > l && a[b[r - 1]] <= a[i]) r--;
        b[r++] = i;
        if(i > n - k + 1) continue;
        Max[i] = a[b[l]];
    }
    for(int i = 1; i <= n; i++) {
        while(r > l && a[c[r - 1]] >= a[i]) r--;
        c[r++] = i;
        if(i < k) continue;
        while(r > l && c[l] < i - k + 1) l++;
        Min[i] = a[c[l]];
    }
}

int main()
{
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    solve();
    for(int i = k; i <= n; i++) printf("%d%c", Min[i], (i == n ? 
 :  ));
    for(int i = 1; i <= n - k + 1; i++) printf("%d%c", Max[i], (i == n - k + 1 ? 
 :  ));
    return 0;
}

 

以上是关于Sliding Window POJ - 2823的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2823 Sliding Window

POJ 2823 Sliding Window

POJ2823 Sliding Window

poj 2823 Sliding Window 题解

POJ#2823. Sliding Window

poj 2823 Sliding Window