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
  • 相关阅读:
    u-boot下延时程序失效的bug调试
    tiny4412u-boot烧写及根文件系统制作(不进入终端问题)
    tiny4412学习笔记-将uboot、zImage、文件系统烧到emmc中 (转)
    Tiny4412 U-BOOT移植(转)
    为何ARM linux会引入Device Tree(转)
    嵌入式开发社区
    基于tiny4412的u-boot移植(二)(转)
    ARM Linux 3.x的设备树(Device Tree)(转)
    HTML基础-------HTML标签(1)
    HTML基础-------最初概念以及相关语法
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7469801.html
Copyright © 2011-2022 走看看