• 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.
  • 相关阅读:
    使用netcraft在线查看网站使用的操作系统和服务器
    Terracotta Web Sessions Tutorial
    JPA2.0和Spring的集成配置方式
    Maven笔记(5) Eclipse和Maven集成
    Maven笔记(2) 常用命令和标准的Maven项目结构
    Maven笔记(4) 构建一个Web Project
    Linux 技巧:让进程在后台可靠运行的几种方法
    You are currently running the HMaster without HDFS append support enabled. This may result in data loss. Please see the
    xtrabackup 安装及应用
    CentOS 6.2 X64上64位Oracle11gR2 静默安装,静默设置监听,静默建库亲自实践记录
  • 原文地址:https://www.cnblogs.com/Xiper/p/4470223.html
Copyright © 2020-2023  润新知