zoukankan      html  css  js  c++  java
  • POJ2823 Sliding Window

    Sliding Window
    Time Limit: 12000MS   Memory Limit: 65536K
    Total Submissions: 62915   Accepted: 17956
    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
    

    Source

     
    【题解】
    单调队列裸题
     
    选C++而不是g++就能不TLE了
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 
     6 inline void read(int &x)
     7 {
     8     x = 0;char ch = getchar(), c = ch;
     9     while(ch < '0' || ch > '9')c = ch, ch = getchar();
    10     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
    11     if(c == '-')x = -x;
    12 }
    13 
    14 const int INF = 0x3f3f3f3f;
    15 const int MAXN = 2000000 + 10;
    16 const int MAXM = 2000000 + 10;
    17 
    18 int num[MAXN],q[MAXN],rank[MAXN],head,tail,n,m;
    19 
    20 int main()
    21 {
    22     read(n), read(m);
    23     for(register int i = 1;i <= n;++ i) read(num[i]);
    24     
    25     head = tail = 1, q[1] = num[1],rank[1] = 1;
    26     
    27     //维护单调递增队列 
    28     for(register int i = 2;i <= m;++ i)
    29     {
    30         while(num[i] <= q[tail] && head <= tail) -- tail;
    31         q[++tail] = num[i];
    32         rank[tail] = i;
    33     }
    34     printf("%d ", q[head]);
    35     for(register int i = m + 1;i <= n;++ i)
    36     {
    37         while(num[i] <= q[tail] && head <= tail) -- tail;
    38         while(i - rank[head] + 1 > m && head <= tail)++ head;
    39         q[++tail] = num[i];
    40         rank[tail] = i;
    41         printf("%d ", q[head]);
    42     }
    43     putchar('
    ');
    44     
    45     head = tail = 1, q[1] = num[1],rank[1] = 1;
    46     
    47     //维护单调递减队列 
    48     for(register int i = 2;i <= m;++ i)
    49     {
    50         while(num[i] >= q[tail] && head <= tail) -- tail;
    51         q[++tail] = num[i];
    52         rank[tail] = i;
    53     }
    54     printf("%d ", q[head]);
    55     for(register int i = m + 1;i <= n;++ i)
    56     {
    57         while(num[i] >= q[tail] && head <= tail) -- tail;
    58         while(i - rank[head] + 1 > m && head <= tail)++ head;
    59         q[++tail] = num[i];
    60         rank[tail] = i;
    61         printf("%d ", q[head]);
    62     }
    63     return 0;
    64 }
    POJ2823
  • 相关阅读:
    Linux安装/卸载软件教程
    X-Window/GNOME/KDE的关系
    GNU和GPL的区别/关系
    VSS迁移详细教程
    VSS+SourceAnywhere for VSS搭建版本控制系统教程
    Windows添加用户和组命令
    Windows开启远程桌面服务(Win10)
    rdesktop安装教程
    Spring控制反转(依赖注入)的最简单说明
    TortioseSVN切换账号教程
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7469801.html
Copyright © 2011-2022 走看看