Sliding Window(POJ-2823)

http://poj.org/problem?id=2823 

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 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

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

 题意:

  n为要输入的整数的个数,k为窗口的大小,窗口每次向后移动一位,窗口每次容纳k个数,找出每次的窗口中数值的最大值和最小值。

如例题中,给定了8个数:1 3 -1 -3 5 3 6 7, 窗口大小为  3.

首次滑动窗口中的数是:1 3 -1  其中的最大值是3,最小值为-1。  窗口向后移动一位

此时滑动窗口中的数是:3 -1 -3 其中的最大值是3,最小值为-3。 窗口向后移动一位 

此时滑动窗口中的数是:-1 -3 5 其中的最大值是5,最小值为-3。 窗口向后移动一位

此时滑动窗口中的数是:-3 5 3  其中的最大值是5,最小值为-3。 窗口向后移动一位

此时滑动窗口中的数是:5 3 6   其中的最大值是6, 最小值为3。  窗口向后移动一位

此时滑动窗口中的数是:3 6 7   其中的最大值是7, 最小值为3。  

思路:

 这里可以用到dequeue双向队列(队首队尾都可以插入删除)。

以要找滑动窗口的最大值为例:  维持队列使其是一个递减的队列,使最大值始终在队首。

将n个数存入数组中,从头遍历数组。当队列中有值时,将队尾元素与此时遍历到的数组的元素a[i]比较;如果队尾元素≤a[i],队尾元素出队,继续比较现在的队尾元素和a[i]大小,直至队尾元素>a[i]或者队列中没有元素,a[i]从队尾入队.......一边遍历数组的元素使其入队,一边判断此时队首的最大值是否属于此时的滑动窗口,不属于的话,就将队首元素出队,继续判断,直至找到属于的此滑动窗口的队首元素,保存此元素为此时滑动窗口的最大值。

代码: 

#include<stdio.h>
#define MAX 10000009
int minnum[MAX],maxnum[MAX];     //分别存储滑动窗口的最小值和最大值
int num[MAX];                    //存储n个数
int minqueue[MAX],maxqueue[MAX]; //最小值的队列和最大值的队列  队列存储的是数组元素的下标!
int main()
{
   int minf=0,mint=0,maxf=0,maxt=0; //最小值队列的队首和队尾,最大值队列的队首和队尾
   int n,k;
   int i=1;
   scanf("%d%d",&n,&k);
   for(i=1;i<=n;i++ )
   {
     scanf("%d",&num[i]);
   }

   for(i=1;i<=n;i++)
   {
      //如果队列中有值并且此时队尾元素值≥数组元素,队尾元素出队
      //因为此队列为了使队首元素是最小值,所以要维持队列是递增
     while(minf < mint && num[minqueue[mint-1]] >= num[i])mint--;
     minqueue[mint]=i;
     mint++;


      //如果队列中有值并且此时队尾元素值≤数组元素,队尾元素出队
      //因为此队列为了使队首元素是最大值,所以要维持队列是递减
     while(maxf < maxt && num[maxqueue[maxt-1]] <= num[i])maxt--;
     maxqueue[maxt]=i;
     maxt++;

 //第一个窗口,要等到此时遍历到的元素个数=窗口容纳的个数时,才能找第一个窗口的最值
 //所以必须下标i≥k,才能找各个滑动窗口的最值
     if(i>=k)   
     {
        //如果用此时遍历的元素下标 - 队首元素 ≥ 窗口大小k 
        //说明此时的队首元素不在滑动窗口中,队首元素出队
        while(i-minqueue[minf]>=k)minf++;
         minnum[i-k+1] = num[minqueue[minf]];

        while(i-maxqueue[maxf]>=k)maxf++;
         maxnum[i-k+1] = num[maxqueue[maxf]];
        
     }  
   }
   
   for(i=1;i<= n-k+1;i++)
    printf("%d ",minnum[i]);
   printf("\n");

   for(i=1;i<=n-k+1;i++)
    printf("%d ",maxnum[i]);
   printf("\n"); 
  
   return 0;
}

but

上面的解题代码中并没有去判断k<=0的情况,当k<=0时候是不能进行上面题解的操作的,但是上面的题解提交竟然可以AC的。

下面的这个是对各个边界进行判断,用Java写的,提交可以AC

 

Java+边界判断代码:


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] data = new int[n + 1];
        for (int i = 0; i < n; i++) {
            data[i] = scanner.nextInt();
        }

        if(n==0||k==0||k>n){
        	System.out.println("");
        	System.out.println("");
        }
        else
        {
        	int[] min=new int[n-k+1];
            int[] max=new int[n-k+1];
            int[] minqueue=new int[n];
            int[] maxqueue=new int[n];
            int minf=0,mint=0,maxf=0,maxt=0;
            for(int i=0;i<n;i++)
            {
                while(minf<mint && data[minqueue[mint-1]]>=data[i])mint--;
                minqueue[mint]=i;
                mint++;
                	
                while(maxf<maxt && data[maxqueue[maxt-1]]<=data[i])maxt--;
                maxqueue[maxt]=i;
                maxt++;
                	
                if(i>=k-1){
                	while(i-minqueue[minf]>=k)minf++;
                	min[i-k+1]=data[minqueue[minf]];
                	while(i-maxqueue[maxf]>=k)maxf++;
                	max[i-k+1]=data[maxqueue[maxf]];
                }
            }
            
            for (int i = 0; i < min.length; i++) {
                 System.out.print(min[i] + " ");
            }
            System.out.println();
            for (int i = 0; i < max.length; i++) {
                 System.out.print(max[i] + " ");
            }
        }
         
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值