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

    Time Limit: 12000MS   Memory Limit: 65536K
    Total Submissions: 50107   Accepted: 14438
    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

    题意:给定长度为N的序列,要求:a[i]~a[i+K-1]中的最小值和最大值

    题解:若求最大值,则维护单调队列的递减,用i来计算MAX[i-K+1],也就是Q[head],注意head要>=i-K+1。最小值类似。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<algorithm>
     7 #include<vector>
     8 #include<queue>
     9 using namespace std;
    10 typedef long long LL;
    11 const int maxn=1000010;
    12 int a[maxn],N,K;
    13 int q[maxn],pos[maxn],h,t,now;
    14 int MAX[maxn],MIN[maxn];
    15 inline void getmax(){
    16     h=t=1; q[1]=a[1],pos[1]=1;
    17     for(int i=2;i<=K-1;i++){
    18         while(h<=t&&a[i]>=q[t]) t--;
    19         q[++t]=a[i]; pos[t]=i;
    20     }
    21     for(int i=K;i<=N;i++){
    22         while(h<=t&&a[i]>=q[t]) t--;
    23         q[++t]=a[i]; pos[t]=i;
    24         while(pos[h]<i-K+1) h++;
    25         MAX[i-K+1]=q[h];
    26     }
    27 }
    28 inline void getmin(){
    29     memset(q,0,sizeof(q)); memset(pos,0,sizeof(pos));
    30     h=t=1; q[1]=a[1],pos[1]=1;
    31     for(int i=1;i<=K-1;i++){
    32         while(h<=t&&a[i]<=q[t]) t--;
    33         q[++t]=a[i]; pos[t]=i;    
    34     }
    35     for(int i=K;i<=N;i++){
    36         while(h<=t&&a[i]<=q[t]) t--;
    37         q[++t]=a[i]; pos[t]=i;    
    38         while(pos[h]<i-K+1) h++;
    39         MIN[i-K+1]=q[h];
    40     }
    41 }
    42 int main(){
    43     scanf("%d%d",&N,&K);
    44     for(int i=1;i<=N;i++) scanf("%d",&a[i]);
    45     getmax();
    46     getmin();
    47     for(int i=1;i<=N-K+1;i++) printf("%d ",MIN[i]);
    48     printf("
    ");
    49     for(int i=1;i<=N-K+1;i++) printf("%d ",MAX[i]);
    50     return 0;
    51 }
  • 相关阅读:
    数据挖掘笔记(2018-03-22发布于知乎)
    使用Graphlab参加Kaggle比赛(2017-08-20 发布于知乎)
    大数据笔记
    2018网易游戏数据挖掘实习生笔试面经
    数据库课程设计之图书借阅管理系统
    居中
    lucene
    eclipse中使用git
    yii
    工作总结
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/5077739.html
Copyright © 2011-2022 走看看