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
  • 相关阅读:
    Django学习之八:forms组件【对form舒心了】
    Django学习之七:Django 中间件
    Django学习之六:Django 常用模块导入记忆
    Django学习之五:Django 之 注意事项及汇总
    Django学习之四:Django Model模块
    工程师基本常识
    Redis详解
    Nginx浅析
    MySQL数据库引擎、事务隔离级别、锁
    浅谈HTTP中GET和POST请求方式的区别
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7469801.html
Copyright © 2011-2022 走看看