zoukankan      html  css  js  c++  java
  • poj2823一道纯单调队列

    Sliding Window

    Time Limit: 12000MS   Memory Limit: 65536K
    Total Submissions: 32099   Accepted: 9526
    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 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
    
    一道纯的单调队列,单调队列的经典题:
     1 #include <iostream>
     2 #include <string.h>
     3 #include <math.h>
     4 using namespace std;
     5 typedef struct abcd
     6 {
     7     int a,i;
     8 }abcd;
     9 typedef struct abcda
    10 {
    11     int b,j;
    12 }abcda;
    13     abcd a[6000000];
    14     abcda b[6000000];
    15     int x[2][6000000];
    16 int main()
    17 {
    18     int n,k;
    19     scanf("%d%d",&n,&k);
    20     int h1,h2,t1,t2;
    21     h1=0;
    22     h2=0;
    23     t1=0;
    24     t2=0;
    25     int i,z;
    26     for(i=0;i<n;i++)
    27     {
    28         scanf("%d",&z);
    29         while(h2<t2&&b[t2-1].b>=z) t2--;
    30         b[t2].b=z;
    31         b[t2++].j=i;
    32         while(h1<t1&&a[t1-1].a<=z) t1--;
    33         a[t1].a=z;
    34         a[t1++].i=i;
    35         if(i<k-1)
    36         continue;
    37        if(i-a[h1].i>=k)h1++;
    38         if(i-b[h2].j>=k)h2++;
    39        x[0][i]=a[h1].a;
    40         x[1][i]=b[h2].b;
    41     }
    42     for(i=k-1;i<n-1;i++)
    43     printf("%d ",x[1][i]);
    44     printf("%d
    ",x[1][i]);
    45     for(i=k-1;i<n-1;i++)
    46     printf("%d ",x[0][i]);
    47     printf("%d
    ",x[0][i]);
    48 }
    View Code
  • 相关阅读:
    i春秋xss平台
    i春秋exec
    bugku 你必须让他停下
    bugku 域名解析
    bugku web3
    bugku 矛盾
    (转)ubuntu下怎么放wifi热点给andriod设备
    (转)如何在 ubuntu 下使用 iNode 客户端
    博客更新啦!!
    HDU 5351 MZL's Border (多校联合第5场1009)
  • 原文地址:https://www.cnblogs.com/ERKE/p/3257060.html
Copyright © 2011-2022 走看看