Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

单调队列,很简单就ac了,贴个短代码。

Posted by 0943047777 at 2011-04-14 10:27:58 on Problem 2823
#include <iostream>

const int maxn = 1000010;
int q[maxn][2],rear,front,i,n,m,j,a[maxn];

void push(int i,bool flag)
{
	if(flag)	while(rear>front&&q[rear-1][0]<a[i+m])	rear--;
	else		while(rear>front&&q[rear-1][0]>a[i+m])	rear--;
	q[rear][0] = a[i+m];
	q[rear][1] = i+m ;
	rear++;
}

int main()
{
	scanf("%d%d",&n,&m);
	for( i = 1 ; i <= n ; i++)
		scanf("%d",&a[i]);
	for( j = 0 ; j < 2 ; j++)
	{
		front = rear = 1 ;
		q[front][1] = 0 ;
		for( i = 1-m ; i <= n-m ; i++)
		{
			if(i>0)	printf("%d ",q[front][0]);
			while(front<rear&&q[front][1]<=i)	front++;
			push(i,j);
		}
		printf("%d\n",q[front][0]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator