zoukankan      html  css  js  c++  java
  • POJ 2823 Sliding Window【单调对列经典题目】

    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 positionMinimum valueMaximum 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

    思路:单调队列的应用
    View Code
    #include<stdio.h>
    #include<string.h>
    #define N 1100006     
    int a[N], p[N]; 
    int main()
    {
            int i, j, n, m;
            scanf("%d%d", &n, &m); 
            memset(a, 0, sizeof(a));
            memset(p, 0, sizeof(p)); 
            for(i=1; i<=n; i++)
                scanf("%d", &a[i]);
            int beg=0, end=0;   //开始取最小值 
            for(i=1; i<m; i++)  //预先处理一下前一段 
            { 
                 while(a[p[end]]>=a[i]&&end>=beg)
                     end--;
                   end++;
                 p[end]=i;
             } 
             for(i=m; i<=n; i++)
             {
                 while(p[beg]<=i-m&&end>=beg)  //这里之前没有控制end>=beg RE的很惨... 
                    beg++; 
                while(a[p[end]]>=a[i]&&end>=beg)
                    end--;
                end++;
                p[end]=i;
                printf("%d ", a[p[beg]]); 
            }
            printf("\n"); 
            beg=0, end=0;   //开始取最大值
            memset(p, 0, sizeof(p));
            for(i=1; i<m; i++)
            {
                while(a[p[end]]<=a[i]&&end>=beg)
                    end--;
                end++;
                p[end]=i;
            }
            for(i=m; i<=n; i++)
            {
                while(p[beg]<=i-m&&end>=beg)
                    beg++;
                while(a[p[end]]<=a[i]&&end>=beg)
                    end--;
                end++;
                p[end]=i;
                printf("%d ", a[p[beg]]);
            }
            printf("\n"); 
    } 
             


  • 相关阅读:
    HDU 2895 编辑距离
    AC自动机
    HDU 1707 简单模拟 Spring-outing Decision
    HDU 1710 二叉树的遍历 Binary Tree Traversals
    Codeforces Round #521 (Div. 3) E. Thematic Contests
    Codeforces Round #521 (Div. 3) D. Cutting Out
    Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
    Codeforces Round #510 (Div. 2) B. Vitamins
    Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
    Codeforces Round #506 (Div. 3) 题解
  • 原文地址:https://www.cnblogs.com/Hilda/p/2633456.html
Copyright © 2011-2022 走看看