博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sliding Window(POJ-2823)
阅读量:4045 次
发布时间:2019-05-25

本文共 4401 字,大约阅读时间需要 14 分钟。

 

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 position Minimum value Maximum 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

 题意:

  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
#define MAX 10000009int 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
=data[i])mint--; minqueue[mint]=i; mint++; while(maxf
=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] + " "); } } }}

转载地址:http://pizci.baihongyu.com/

你可能感兴趣的文章
WPF UI&控件免费开源库
查看>>
QT打开项目提示no valid settings file could be found
查看>>
Win10+VS+ESP32环境搭建
查看>>
Ubuntu+win10远程桌面
查看>>
flutter-实现圆角带边框的view(android无效)
查看>>
android 代码实现圆角
查看>>
flutter-解析json
查看>>
android中shader的使用
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>