zoukankan      html  css  js  c++  java
  • poj-2823 Sliding Window

    .

    Sliding Window
    Time Limit: 12000MS   Memory Limit: 65536K
    Total Submissions: 79083   Accepted: 22340
    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 <algorithm>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <string>
     7 #include <cstdlib>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 #include <vector>
    12 #include <map>
    13 #include <list>
    14 #include <iomanip>
    15  #include <fstream>
    16 using namespace std;
    17 typedef long long ll;
    18 const int maxn=1e6;
    19 int a[maxn+1],q1[maxn+1],q2[maxn+1],maxnn[maxn+1],minn[maxn+1]; 
    20 int main()
    21 {
    22     int n,k,x;
    23     scanf("%d%d",&n,&k);
    24     for(int i=1;i<=n;++i)
    25     {
    26         scanf("%d",&a[i]);
    27     }
    28     int h1=1,h2=1,t1=0,t2=0;
    29     for(int i=1;i<=n;++i)
    30     {    
    31         while(h1<=t1&&a[q1[t1]]<=a[i])//当长度为k的队列头部没有超过尾部的时候,并且这个队列尾部的下标所代表的值比当前遍历到的a[i]要小,那么队尾收缩 
    32             t1--;
    33         q1[++t1]=i;//队列尾指针向后移动一位,并且队列尾部下标为当前遍历到的下标i。    
    34         while(i-k>=q1[h1])//当前的 i- 滑块长度k  所得到的下标 不小于当前头指针下标。 5-3 =2  
    35             h1++;       //那么长度为k的队列头指针向后移动,直到  h~i 的长度不超过要求的滑块长度k。 删队首 
    36         maxnn[i]=a[q1[h1]]; //当前滑块的最大值就是长度为k的队列头指针所指向的下标所代表的值。
    37         
    38         
    39         while(h2<=t2&&a[q2[t2]]>=a[i])
    40             t2--;
    41         q2[++t2]=i;
    42         while(i-k>=q2[h2])
    43             h2++;
    44         minn[i]=a[q2[h2]];
    45         
    46     }
    47     
    48     for(int i=k;i<=n;++i)
    49         printf("%d ",minn[i]);
    50     printf("
    ");
    51     for(int i=k;i<=n;++i)        
    52         printf("%d ",maxnn[i]);
    53 
    54     return 0;
    55 }
    View Code

    ..

  • 相关阅读:
    css文本垂直水平居中
    如何通过eclipse查看、阅读hadoop2.4源码
    hadoop、storm和spark的区别、比较
    Spark学习体系整理(基础篇、中级篇、高级篇所涉及内容)
    scala class和object,trait的区别
    Scala的=>作用
    [Scala函数特性系列]——按名称传递参数
    Python读写文件
    如何向map和reduce脚本传递参数,加载文件和目录
    Java中字符串中子串的查找共有四种方法(indexof())
  • 原文地址:https://www.cnblogs.com/greenaway07/p/11217565.html
Copyright © 2011-2022 走看看