码迷,mamicode.com
首页 > Windows程序 > 详细

Sliding Window POJ - 2823

时间:2018-07-31 15:27:22      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:inf   ==   div   define   stream   strong   tor   algo   main   

技术分享图片

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

//#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 ? \n :  ));
    for(int i = 1; i <= n - k + 1; i++) printf("%d%c", Max[i], (i == n - k + 1 ? \n :  ));
    return 0;
}

 

Sliding Window POJ - 2823

标签:inf   ==   div   define   stream   strong   tor   algo   main   

原文地址:https://www.cnblogs.com/zgglj-com/p/9395427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!