poj 2823(单调队列)

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
   
Case Time Limit: 5000MS

Description

An array of size  n ≤ 10 6 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
 
 

给定一个大小已知的数组以及一个大小已知的滑动窗口,窗口每个时刻向后移动一位,求出每个时刻窗口中数字的最大值和最小值。

这个题是单调队列的入门题。

求最大值:建立一个单调递减队列,元素从左到右依次入队,入队之前必须从队列尾部开始删除那些比当前入队元素小或者相等的元素,直到遇到一个比当前入队元素大的元素,或者队列为空为止。若此时队列的大小超过窗口值,则从队头删除元素,直到队列大小小入窗口值为止。然后把当前元素插入队尾。

求最小值:建立一个单调递增队列,元素从左到右依次入队,入队之前必须从队列发问开始删除那些比当前入队元素大或者相等的元素,直到遇到一个比当前入队元素小的元素,或者队列为空为止。若此时队列的大小超过窗口值,则从队头删除元素,直到队列大小小入窗口值为止。然后把当前元素插入队尾。

AC:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn= 1000005;
struct node
{
	int val;
	int pos;
};
int n,k,win[maxn],maxhead,maxtail,minhead,mintail;
int Max[maxn],Min[maxn],cnt;
node maxque[maxn],minque[maxn];

void initQue()
{
	maxhead = maxtail = 0;
	minhead = mintail = 0;
	cnt = 0;
	for(int i = 1; i <= k; i++)
	{
		while(maxhead != maxtail && win[i] > maxque[maxtail-1].val) maxtail--;
		maxque[maxtail].val = win[i];
		maxque[maxtail].pos = i;
		maxtail++;
		while(minhead != mintail && win[i] < minque[mintail-1].val) mintail--;
		minque[mintail].val = win[i];
		minque[mintail].pos = i;
		mintail++;
	}
	Max[++cnt] = maxque[maxhead].val;
	Min[cnt] = minque[minhead].val;
}

void Sliding_Window()
{
	for(int i = k+1; i <= n; i++)
	{
		//找出当前区间最大值 
		while(maxhead != maxtail && win[i] > maxque[maxtail-1].val) maxtail--;
		maxque[maxtail].val = win[i];
		maxque[maxtail].pos = i;
		maxtail++;
		while(maxhead != maxtail && i - maxque[maxhead].pos + 1 > k) maxhead++;
		Max[++cnt] = maxque[maxhead].val;
		//找出当前区间最小值
		while(minhead != mintail && win[i] < minque[mintail-1].val) mintail--;
		minque[mintail].val = win[i];
		minque[mintail].pos = i;
		mintail++;
		while(minhead != mintail && i - minque[minhead].pos + 1 > k) minhead++;
		Min[cnt] = minque[minhead].val;
	}
}

void print()
{
	for(int i = 1; i <= cnt; i++)
		printf("%d ",Min[i]);
	printf("\n");
	for(int i = 1; i <= cnt; i++)
		printf("%d ",Max[i]);
	printf("\n");
}

int main()
{	
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		for(int i = 1; i <= n; i++)
			scanf("%d",&win[i]);
		initQue();
		Sliding_Window();
		print();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值