• poj 2823 Sliding Window 单调队列


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

     

    [Submit]   [Go Back]   [Status]   [Discuss]

    Home Page   Go Back  To top

     

     

     1 /*
     2    单调队列基础题。
     3 */
     4 /*
     5 单调队列维护。
     6 以单调递减队列为例。10 9 8 7 6 ...
     7 初始化
     8 head = 0; tail = -1;
     9 进队列:从后往前查找。满足  while( head<=tail  && q[ tail ].rp < tmp.rp ) tail --;
    10                                             此时相当于舍弃 tail 原来保存到的数据。
    11 出队列:从前往后查找。满足  while( head<=tail  && q[ tail ].num <= conunt ) head ++;
    12                                             此时相当于避免比当前值先进来,却又比较大的值,被输出。
    13 */
    14 
    15 #include<iostream>
    16 #include<stdio.h>
    17 #include<cstring>
    18 #include<cstdlib>
    19 using namespace std;
    20 
    21 int a[1000002];
    22 typedef struct
    23 {
    24     int num;
    25     int rp;
    26 }Queue;
    27 Queue q[1000002];
    28 int main()
    29 {
    30     int n,i,k,flag;
    31     int Num,conunt,head,tail;
    32     Queue tmp;
    33     while(scanf("%d%d",&n,&k)>0)
    34     {
    35         Num=0;
    36         conunt=0;
    37         head = 0; tail = -1;
    38         flag=1;
    39 
    40         for(i=1;i<=n;i++)
    41         {
    42             scanf("%d",&a[i]);
    43             tmp.rp=a[i];
    44             tmp.num=++Num;
    45             while( head<=tail && q[tail].rp > tmp.rp)
    46                 tail--;
    47             q[++tail]=tmp;
    48             if(i>=k)
    49             {
    50                 while( head<=tail && q[head].num<=conunt)
    51                     head ++;
    52                 if(head>tail) ;
    53                 else if(flag==1)
    54                 {
    55                     printf("%d",q[head].rp);
    56                     flag=0;
    57                 }
    58                 else printf(" %d",q[head].rp);
    59                 conunt++;
    60             }
    61         }
    62         printf("
    ");
    63         Num=0;
    64         conunt=0;
    65         head = 0; tail = -1;
    66         flag=1;
    67         for(i=1;i<=n;i++)
    68         {
    69             tmp.rp=a[i];
    70             tmp.num=++Num;
    71             while( head<=tail && q[tail].rp < tmp.rp)
    72                 tail--;
    73             q[++tail]=tmp;
    74             if(i>=k)
    75             {
    76                 while( head<=tail && q[head].num<=conunt)
    77                     head ++;
    78                 if(head>tail) ;
    79                 else if(flag==1)
    80                 {
    81                     printf("%d",q[head].rp);
    82                     flag=0;
    83                 }
    84                 else printf(" %d",q[head].rp);
    85                 conunt++;
    86             }
    87         }
    88         printf("
    ");
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    如何解决使用webpack打包之后,font-awsome路径不对的问题,终极解决方法
    vue-route+webpack部署单页路由项目,访问刷新出现404问题
    vue2.0打包webpack出错
    Coding Pages jekyll 404无法找到静态文件(css,js )
    git常用命令及分支简介
    【git常见问题】fatal: Not a valid object name: 'master'.
    jekyll下载了模版运行出现问题cannot load such file -- jekyll-paginate (LoadError)
    Jekyll解决Deprecation: but you haven't included the `jekyll-paginate` gem
    06四则运算3
    09NABCD需求分析
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3554929.html
Copyright © 2020-2023  润新知