美文网首页
【单调队列】POJ_2823_Sliding Window

【单调队列】POJ_2823_Sliding Window

作者: 今天也继续开心涅普涅普 | 来源:发表于2016-10-22 17:58 被阅读0次

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 55654 Accepted: 16011
Case Time Limit: 5000MS

Description
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 position Minimum value Maximum value
[1 3 -1] -3 5 3 6 7 -1 3
1 [3 -1 -3] 5 3 6 7 -3 3
1 3 [-1 -3 5] 3 6 7 -3 5
1 3 -1 [-3 5 3] 6 7 -3 5
1 3 -1 -3 [5 3 6] 7 3 6
1 3 -1 -3 5 [3 6 7] 3 7
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
POJ Monthly--2006.04.28, Ikki

题意:
有一个n个元素的整数序列,一个能容纳k个元素的滑动窗口,窗口从头滑至尾,求每一个区间中的最小数和最大数。

思路:
单调队列。维护两个单调队列,分别严格单调增、严格单调减,若队头元素距离当前元素超过k,则需出队,该队头元素既是区间最大/最小。

#include<cstdio>
#include<deque>
using namespace std;

const int maxn = 1000000;

struct Node {
    int value, pos;
    Node(int v = -1, int p = -1) :value(v), pos(p) {};
};

deque<Node> minque;
deque<Node> maxque;
int maxans[maxn + 10];
int minans[maxn + 10];

int main() {
    int n, m, th = 0;
    int num;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &num);
        if (i >= m) {
            minans[th] = minque.front().value;
            maxans[th++] = maxque.front().value;
            // 除去不属于同一区间的元素
            while (!minque.empty() && i - minque.front().pos >= m)
                minque.pop_front();
            while (!maxque.empty() && i - maxque.front().pos >= m)
                maxque.pop_front();
        }
        // 入队,维护队列单调
        while (!minque.empty() && minque.back().value >= num)
            minque.pop_back();
        minque.push_back(Node(num, i));
        while (!maxque.empty() && maxque.back().value <= num)
            maxque.pop_back();
        maxque.push_back(Node(num, i));
    }
    minans[th] = minque.front().value;
    maxans[th++] = maxque.front().value;
    for (int i = 0; i < th - 1; ++i)
        printf("%d ", minans[i]);
    printf("%d\n", minans[th - 1]);
    for (int i = 0; i < th - 1; ++i)
        printf("%d ", maxans[i]);
    printf("%d\n", maxans[th - 1]);
    return 0;
}

相关文章

  • 【单调队列】POJ_2823_Sliding Window

    Sliding WindowTime Limit: 12000MS Memory Limit: 65536...

  • 单调队列与Sliding Window

    所谓单调队列——即元素具有单调性且同时保持着队列性质的数据结构。这个数据结构使用频率不是很高,笔者在之前也没有接触...

  • 单调队列

    队列中各元素序列是严格单调递增或递减的,队首和队尾都可以出队,但入队只能从队尾入队。由于队列内元素有序,取最值的复...

  • 单调队列

    42. 接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。...

  • 单调队列

    原题链接[https://www.acwing.com/problem/content/137/] 给定一个长度为...

  • 单调队列

    单调队列,也可以叫做Monotonic Queue这种数据结构主要可以优化能够用max/min heap 解决的题...

  • 单调队列&单调栈

    就是一些很神奇的数据结构 A:最大矩形 题目: 给一个直方图,求直方图中的最大矩形的面积。例如,下面这个图片中直方...

  • 动态规划之单调队列

    单调队列的性质 单调队列中的元素单调递减或单调递增 只能在队尾插入,可以从两头删除 其目的就是为了保持队列中元素的...

  • Algorithm小白入门 -- 队列和栈

    队列和栈队列实现栈、栈实现队列单调栈单调队列运用栈去重 1. 队列实现栈、栈实现队列 队列是一种先进先出的数据结构...

  • 单调 栈&&队列

    题目链接:Largest Rectangle in a Histogram 单调栈: 题目链接:Imbalance...

网友评论

      本文标题:【单调队列】POJ_2823_Sliding Window

      本文链接:https://www.haomeiwen.com/subject/acshuttx.html