• UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>


    K - Sliding Window

    Time Limit: 18000/6000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others)
     

    An array of size n106 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 position Minimum value Maximum value

    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 and output

    Sample InputSample Output
    8 3
    1 3 -1 -3 5 3 6 7
    -1 -3 -3 -3 3 3
    3 3 5 5 6 7

    Hint

    The data used in this problem is unofficial data prepared by love8909. So any mistake here does not imply mistake in the offcial judge data.

    解题报告

    滑动窗口问题,我们可以在O(1)的时间内得到某个点的答案,就是维护一个单调队列,首先考虑最大值问题,我们考虑 i < j,且a[i] < a[j],显然可以得到a[i]是根本无用的(因为从左往右滑,a[j]未出之前a[i]根本不可能最优),因此我们只需维护一个单调递减的队列的即可,即可在O(1)的时间内得到某个点最优值.

    插入时也要维护单调性,不再累述.

    最小值同理.

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    const int maxn = 1e6 + 50;
    int n,k,q[maxn],h[maxn];
    
    int main(int argc,char *argv[])
    {
      scanf("%d%d",&n,&k);
      int front = 0 , rear = 0 ;
      for(int i = 0 ; i < n ; ++ i)
       scanf("%d",&h[i]);
      // Judge
      if (k >= n)
       k = n;
      // Init
      q[rear++] = 0;
      for(int i = 1 ; i < k ; ++ i)
       {
              while(h[i] <= h[q[rear-1]] && front < rear)
             rear--;
           q[rear++] = i; 
       }
      printf("%d",h[q[front]]);
      for(int i = k ; i < n ; ++ i)
       {
              while(front < rear && i - q[front] >= k)
               front++;
              while(h[i] <= h[q[rear-1]] && front < rear)
             rear--;
           q[rear++] = i;
           printf(" %d",h[q[front]]);
       }
      printf("
    ");
      // ReInit
      front = 0 , rear = 0 , q[rear++] = 0;
      for(int i = 1 ; i < k ; ++ i)
       {
              while(h[i] >= h[q[rear-1]] && front < rear)
             rear--;
           q[rear++] = i; 
       }
      printf("%d",h[q[front]]);
      for(int i = k ; i < n ; ++ i)
       {
              while(front < rear && i - q[front] >= k)
               front++;
              while(h[i] >= h[q[rear-1]] && front < rear)
             rear--;
           q[rear++] = i;
           printf(" %d",h[q[front]]);
       }
      printf("
    ");
      return 0;
    }

     

    No Pain , No Gain.
  • 相关阅读:
    c++基类和派生类的框架
    从文件读入7个数并排序
    字符串复制函数-简单
    c++函数模板-简单
    用初始化列表解决常私有变量问题-简单
    分配和释放内存-简单
    结构体小程序-简单
    转化的力量
    初次创业需要注意的几点
    重生的2014下半年的起点
  • 原文地址:https://www.cnblogs.com/Xiper/p/4470223.html
Copyright © 2020-2023  润新知