POJ 2823 单调队列

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 59304 Accepted: 17027
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

题目大意:

给出一个数列,求每一个长度为k区间的最大值、最小值


解题思路:

先只考虑求最大值:

显然,当区间变化的时候,最大值可能也会改变。如果我们能够维护一个数组,求出当前位置后面的最大值,那么问题会简单许多。

单调队列,维护一个队列d,和l(头指针)、r(尾指针),d[i]表示d[i-1]位置后的最大值(随着区间变化不断更新)。每当一个新的数入队时,从后向前找到比它大的第一个数,删除后面的数列,插入该数。出队时直接l++。

先预处理出第一个区间的d数组(最左),当区间右移时(相当于把一个数加入d数组)入队,当区间覆盖过最大值时出队。

最小值同理。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define LL long long

const int N=1000010;
const int MAX=1e9+7;
int n,a[N],m;
int d[N],l,r;

inline void R(int &v)//读入、输出优化 
{
	v=0;
	char ch=getchar();
	int f=0;
	while(!isdigit(ch)){if(ch=='-')f=1;ch=getchar();}
	while(isdigit(ch)){v=(v<<3)+(v<<1)+ch-'0';ch=getchar();}
	if(f) v=-v;
}
namespace ib {char b[100];}
inline void P(int x)
{
    if(x==0) {putchar(48); return;}
    if(x<0) {putchar('-'); x=-x;}
    char *s=ib::b;
    while(x) *(++s)=x%10, x/=10;
    while(s!=ib::b) putchar((*(s--))+48);
}

void push(int x)//入队操作 
{
	int i,j;
	for(i=r;i>=l;i--) if(x<=d[i]) break;//二分可能更快(?) 
	r=i;
	d[++r]=x;
}

void init()
{
	int i,j;
	l=1,r=1;
	memset(d,0,sizeof(d));
	int maxx=-MAX,b;
	for(i=1;i<=m;++i)
	  if(a[i]>=maxx) maxx=a[i],b=i;//保留相同的数避免出队时删除错误 
	d[r]=maxx;
	for(i=b+1;i<=m;++i) push(a[i]);
}

void shan()//出队操作 
{
	l++;
}

void _push(int x)//博主只会Ctrl+C所以有两遍(哈?)
{
	int i,j;
	for(i=r;i>=l;i--) if(x>=d[i]) break;
	r=i;
	d[++r]=x;
}

void _init()
{
	int i,j;
	l=1,r=1;
	memset(d,0,sizeof(d));
	int minn=MAX,b;
	for(i=1;i<=m;++i)
	  if(a[i]<=minn) minn=a[i],b=i;
	d[r]=minn;
	for(i=b+1;i<=m;++i) _push(a[i]);
}

void _shan()
{
	l++;
}

int main()
{
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	
	int i,j;
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%d",&m);
		for(i=1;i<=n;i++)
		  R(a[i]);
		_init();
		P(d[l]);putchar(' ');
		for(i=2;i<=n-m+1;++i)
		{
	        if(a[i-1]==d[l])_shan();
			_push(a[i+m-1]);;
			P(d[l]);putchar(' ');
		}
		
		printf("\n");
		
		init();
		P(d[l]);putchar(' ');
		for(i=2;i<=n-m+1;++i)
		{
	        if(a[i-1]==d[l])shan();
			push(a[i+m-1]);
			P(d[l]);putchar(' ');
		}

	}
	
	return 0;
}

结语:

*单调队列

典型的队列思想

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
摘 要 伴随着人才教学的关注度越来越高,对于人才的培养也是当今社会发展的最为重要的问题之一。为了进一步的进行人才的培养关工作,许多的学校或者是教育的机构逐步的开展了网络信息化的教学和和管理工作,通过信息化的手段和技术实现网络信息化的教育及管理模式,通过网络信息化的手段实现在线答题在线考试和学生信息在线的管理等操作。这样更加的快捷解决了人才培养之中的问题,也在进一步的促进了网络信息化教学方式的快速的发展工作。相较于之前的人才教育和培养工作之中,存在这许多的问题和局限性。在学生信息管理方面通过线下管理的形式进行学生信息的管理工作,在此过程之中存在着一定的局限性和低效性,往往一些突发的问题导致其中工作出现错误。导致相关的教育工作受到了一定的阻碍。在学生信息和学生成绩的管理方面,往常的教育模式之下都是采用的是人工线下的进行管理和整理工作,在这一过程之中存在这一定的不安全和低效性,面对与学生基数的越来越大,学生的信息管理也在面领着巨大的挑战,管理人员面领着巨大的学生信息的信息量,运用之前的信息管理方式往往会在统计和登记上出现错误的情况的产生,为后续的管理工作造成了一定的困难。然而通过信息化的管理方式进行对学生信息的管理不仅可以避免这些错误情况的产生还可以进一步的简化学生信息管理工作的流程,节约了大量的人力和物力的之处。在线答题系统的实现不仅给学生的信息管理工作和在线考试带来了方便也进一步的促进了教育事业信息化的发展,从而实现高效化的教学工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值