美文网首页
【单调队列】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

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