poj 2823 - Sliding Window【单调队列模板】

 

Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 55797 Accepted: 16048
Case Time Limit: 5000MS

 

Description

An array of size  n ≤ 10 6 is given to you. There is a sliding window of size  k which is moving from the very left of the array to the very right. You can only see the  k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is  [1 3 -1 -3 5 3 6 7], and  k is 3.
Window positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input

The input consists of two lines. The first line contains two integers  n and  k which are the lengths of the array and the sliding window. There are  n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

 

这道题算是单调队列的模板吧,单调队列解决的是有一个长度为n的数列,有数据一个长度为k的窗口,求窗口在滑动的时候每个窗口里的极值。一般的方法会忽略到上个窗口所在位置的数据,造成浪费。利用单调对列,窗口每进行一次滑动,就将新加入窗口的数字进行判断,查看他在队列的位置。以区间最大值为例子,每次查找他在队列中比他小的数据,把他们全部删去,因为所要的是最大值,他们在该区域上已经没有任何价值。然后新加入窗口的数放到尾部。这样每次,只要查询最大值就可以了。

#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
//typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> PAI;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 10;
int minn[MAXN], maxx[MAXN], a[MAXN];
int n, k, loc[MAXN], que[MAXN];
void get_min() {
    int head = 1, tail = 0;
    for (int i = 0; i < k - 1; i++) {
        //严格递减
        while (head <= tail && a[i] <= que[tail]) tail--;
        que[++tail] = a[i];
        loc[tail] = i;
    }
    for (int i = k - 1; i < n; i++) {
        while (head <= tail && a[i] <= que[tail]) tail--;
        que[++tail] = a[i];
        loc[tail] = i;
         while (loc[head] < i - k + 1) head++;
        minn[i - k + 1] = que[head];
    }
}
void get_max() {
    int head = 1, tail = 0;
    for (int i = 0; i < k - 1; i++) {
        //严格递减
        while (head <= tail && a[i] >= que[tail]) tail--;
        que[++tail] = a[i];
        loc[tail] = i;
    }
    for (int i = k - 1; i < n; i++) {
        while (head <= tail && a[i] >= que[tail]) tail--;
        que[++tail] = a[i];
        loc[tail] = i;
         while (loc[head] < i - k + 1) head++;
        maxx[i - k + 1] = que[head];
    }
}
void solve() {
    get_max(); get_min();
    for (int i = 0; i < n - k; i++) {
        printf("%d ", minn[i]);
    }
    printf("%d\n", minn[n - k]);
    for (int i = 0; i < n - k; i++) {
        printf("%d ", maxx[i]);
    }
    printf("%d\n", maxx[n - k]);
}
int main() {
    while (scanf("%d%d", &n, &k) != EOF) {
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
        }
        solve();
    }
    return 0;
}
/*
8 3
1 3 -1 -3 5 3 6 7
*/

 



 

转载于:https://www.cnblogs.com/cniwoq/p/6770766.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值