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

用deque模拟一下单调队列,写起来很简单

Posted by yu_xuan at 2021-09-05 15:00:56 on Problem 2823
#include<stdio.h>
#include<deque>
using std::deque;
const int N=1e6+3;
int a[N];
deque<int> q1,q2;
int main()
{
	int n,k;scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
	{
		while(!q1.empty()&&a[q1.back()]>a[i])q1.pop_back();
		q1.push_back(i);
		if(!q1.empty()&&q1.front()<i-k+1)q1.pop_front();
		if(i>=k)printf("%d%c",a[q1.front()],i==n?'\n':' ');
	}
	for(int i=1;i<=n;i++)
	{
		while(!q2.empty()&&a[q2.back()]<a[i])q2.pop_back();
		q2.push_back(i);
		if(!q2.empty()&&q2.front()<i-k+1)q2.pop_front();
		if(i>=k)printf("%d%c",a[q2.front()],i==n?'\n':' ');
	}
}

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