POJ2823 滑动窗口 单调队列

来源:互联网 发布:android ble 接收数据 编辑:程序博客网 时间:2024/04/28 01:57

An array of size n ≤ 106 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 31 3 -1 -3 5 3 6 7

Sample Output

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

曾经在leetcode上做过这个题目,但是,为了学习一种新的数据结构,单调队列,重新做了这道题目

单调队列,需要维护,一个pair,分别是元素的pos,和val,队列内部是按照,val升序(或降序)排列的,我们维护最小值,因此,队尾元素入队的时候,可以删除比他大的所有元素,同时判断队首元素的pos是不是超出了范围,即时的pop_front(),最后C++过了,G++ TLE,莫名其妙。
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;#define N 1000007#include <deque>int n, k;int a[N];struct Node {    int pos,val;    Node(){}    Node(int p,int v):pos(p), val(v) {}};int main() {    //freopen("in.txt", "r", stdin);    while (~scanf("%d%d",&n, &k)) {        for (int i=0; i<n; i++) {            scanf("%d", &a[i]);        }        deque<Node> d;        //max        for (int i=0; i<k; i++) {            //加入队尾            while (d.empty() == false) {                if (d.back().val >= a[i]) {                    d.pop_back();                } else {                    break;                }            }            d.push_back(Node(i, a[i]));        }        printf("%d ",d.begin()->val);        for (int i=k; i<n; i++) {            //加入队尾            while (d.empty() == false) {                if (d.back().val >= a[i]) {                    d.pop_back();                } else {                    break;                }            }            d.push_back(Node(i, a[i]));            //处理队首            while (d.empty() == false) {                if (i - d.begin()->pos >= k) {                    d.pop_front();                } else {                    break;                }            }            printf("%d ",d.begin()->val);        }        d.clear();        printf("\n");        //min        for (int i=0; i<k; i++) {            //加入队尾            while (d.empty() == false) {                if (d.back().val <= a[i]) {                    d.pop_back();                } else {                    break;                }            }            d.push_back(Node(i, a[i]));        }        printf("%d ",d.begin()->val);        for (int i=k; i<n; i++ ) {            //加入队尾            while (d.empty() == false) {                if (d.back().val <= a[i]) {                    d.pop_back();                } else {                    break;                }            }            d.push_back(Node(i, a[i]));            //处理队首            while (d.empty() == false) {                if (i - d.begin()->pos >= k) {                    d.pop_front();                } else {                    break;                }            }            printf("%d ",d.begin()->val);        }        d.clear();    }}







0 0