zoukankan      html  css  js  c++  java
  • Sliding Window 单调队列求区间内的最值

    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

    ***************************************************************************************************************************

    单调队列,求区间内的最值(注意是多个定长区间)

    ***************************************************************************************************************************

     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 #include<algorithm>
     7 using namespace std;
     8 int a[1000001];
     9 int que[100000001];
    10 int n,k;
    11 void Min()//单调递增求最小值
    12 {
    13     int head=1,tail=0;
    14     int it;
    15     for(it=0;it<k-1;it++)
    16     {
    17         while(head<=tail&&a[que[tail]]>=a[it])tail--;
    18         tail++;
    19         que[tail]=it;
    20     }
    21     for(it=k-1;it<n;it++)
    22     {
    23         while(head<=tail&&a[que[tail]]>=a[it])tail--;
    24         tail++;
    25         que[tail]=it;
    26         while(que[head]<it-k+1)head++;
    27         printf("%d",a[que[head]]);
    28         printf("%c",((it==n-1)?'
    ':' '));
    29 
    30     }
    31 }
    32 void Max()//单调递减,求最大值
    33 {
    34     int head=1,tail=0;
    35     int it;
    36     for(it=0;it<k-1;it++)
    37     {
    38         while(head<=tail&&a[que[tail]]<=a[it])tail--;
    39         tail++;
    40         que[tail]=it;
    41     }
    42     for(it=k-1;it<n;it++)
    43     {
    44         while(head<=tail&&a[que[tail]]<=a[it])tail--;
    45         tail++;
    46         que[tail]=it;
    47         while(que[head]<it-k+1)head++;
    48         printf("%d",a[que[head]]);
    49         printf("%c",((it==n-1)?'
    ':' '));
    50 
    51     }
    52 }
    53 int main()
    54 {
    55     int i,j;
    56     while(scanf("%d%d",&n,&k)!=EOF)
    57     {
    58         for(i=0;i<n;i++)
    59         scanf("%d",&a[i]);
    60         Min();
    61         Max();
    62     }
    63     return 0;
    64 
    65 }
    View Code
  • 相关阅读:
    gsoap、c++。webservice的client。
    2.5给定两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。进阶:假设这些数位是正向存放的。
    c++、webServices、gsoap、tinyxml、iconv
    2.4编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或者等于x的结点之前。
    CMD窗口快捷键
    IE7下position:relative与overflow的问题
    关于ASP.NET下,JQuery+AJAX使用JSON返回对象集合List数据的总结
    找不到可安装的 ISAM(必解决)
    jquery mini ui
    Unity3D
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3450728.html
Copyright © 2011-2022 走看看