比赛 Segment Tree Competition 评测结果 AAAAAAAAA
题目名称 滑动窗口 最终得分 100
用户昵称 派特三石 运行时间 0.964 s
代码语言 C++ 内存使用 6.53 MiB
提交时间 2016-08-28 19:05:05
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<deque>
using namespace std;
const int maxn=1000010;
int n,k,s[maxn];
int ma[maxn],mi[maxn];
int init()
{
	freopen("window.in","r",stdin);
	freopen("window.out","w",stdout);
	deque<int> up;
	deque<int> down;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&s[i]);	
	}
	for(int i=1;i<k;++i)
	{
		while(!up.empty()&&s[i]<=s[up.back()])
		{
			up.pop_back();
		}up.push_back(i);
		while(!down.empty()&&s[i]>=s[down.back()])
		{
			down.pop_back();
		}
		down.push_back(i);
	}
	for(int i=k;i<=n;++i)
	{
		while(!up.empty()&&s[i]<=s[up.back()])
		{
			up.pop_back();
		}
		up.push_back(i);
		while(!down.empty()&&s[i]>=s[down.back()])
		{
			down.pop_back();
		}
		down.push_back(i);
		if(!up.empty()&&up.front()<i-k+1)
		{
			up.pop_front();
		}
		if(!down.empty()&&down.front()<i-k+1)
		{
			down.pop_front();
		}
		mi[i-k+1]=s[up.front()];
		ma[i-k+1]=s[down.front()];
	}
	for(int i=1;i<=n-k+1;++i)
	{
		printf("%d ",mi[i]);
	}
	printf("\n");
	for(int i=1;i<=n-k+1;++i)
	{
		printf("%d ",ma[i]);
	}
}
int you=init();
int main()
{
	;
}