poj 2823 Sliding Window(滑动窗口,单调队列)

Sliding Window

Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 76785 Accepted: 21745
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 -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,n<=10^6 n为又多少个数,k<=n 为窗口大小。第二行为有n个数,求,从左到右,有一个长度为k的窗口滑动,第一行输出窗口的最小值,第二行输出窗口的最大值

思路:这道题,就是单调队列的模板题,长度为k的滑动窗口。求每一个长度为k的区间内的最大值和最小值

单调队列:

用双端队列维护,双端队列中一般维护一个结构体,结构体中有当前下标,和值的大小

当求最大值时 deque头部位最大值,deque中从头部到尾部,下标id为递增,val值递减。保持这样的单调性,维护当前k长度区间内的最大值,最大值也就在头部

在维护过程中需要注意的3点:

1、当队列不为空时,若当前deque头部元素的下标不在当前要求的长度为k的区间内,deque需要把头部元素pop掉

2、插入元素时,都是从尾部插入的,当从尾部插入插入该值时,需要和当前尾部元素对比一下,若大于当前尾部元素的值,因求最大值,所以在维护这个值就没啥意义了,所以pop掉 

3.、要求当前长度为k区间内的最大值,需要正好把长度为k的区内的元素插入完,那么当前deque中头部元素,就是要求的值

视频详解:https://www.bilibili.com/video/av23189029?from=search&seid=8438597575520741840

代码:

//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<deque>
using namespace std;
#define Max 1000010
int n,k,sum;
struct node
{
	int id,val;
}stu[Max];
deque<node> de;
deque<node> di;
int Mi[Max];
int Ma[Max]; 
void getMaxMin()
{
	sum = 0;
	for(int i = 1;i<=n;i++)     // 求长度k的区间内的最大值,双端队列中从头到尾,保持单调性,id 递增,值递减 
	{	
		while(!de.empty()&&i-de.front().id>=k)  //deque的头元素的id不在当前的求的k长度区间内。所以在pop掉  
				de.pop_front();
		while(!de.empty()&&stu[i].val>de.back().val) //当从尾部插入插入该值时,需要和当前尾部元素对比一下, 
				de.pop_back();               //若大于当前尾部元素,因求最大值,所以在维护这个值就没啥意义了,所以pop掉 
		de.push_back(stu[i]);
		if(i>=k)
			Ma[sum] = de.front().val;
		// 求长度k的区间内的最小值,deque中从头到尾保持单调性,id递增,val也是递增;
		while(!di.empty()&&i-di.front().id>=k)
			di.pop_front();
		while(!di.empty()&&stu[i].val<di.back().val)
			di.pop_back();
		di.push_back(stu[i]);
		if(i>=k){
			Mi[sum++] = di.front().val;
		}
	}
	
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1;i<=n;i++){
		scanf("%d",&stu[i].val);
		stu[i].id = i;
	}
	//getMin();
	getMaxMin();
	for(int i = 0;i<sum;i++){
		if(i) printf(" ");
		printf("%d",Mi[i]);
	}
	printf("\n");
	for(int i = 0;i<sum;i++){
		if(i) printf(" ");
		printf("%d",Ma[i]);
	}
	printf("\n");
	return 0;
} 
	
	

其实这题加个输入输出优化,能减到不少时间

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值