题目链接
http://poj.org/problem?id=2823
【题解】
这个题目就是纯模板题,但是这个题目有很多坑点,首先这个题目是多组输入的,然后这个输出还卡我们时间
#include<stdio.h>
#include<iostream>
using namespace std;
const int N = 1e6+10;
int IM[N],Im[N];
int a[N],n,k;
int ans1[N],ans2[N];
int main()
{
while(scanf("%d%d",&n,&k)!=EOF){
int head1=1,head2=1;
int tail1=0,tail2=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
while( head1 <= tail1 && a[IM[tail1]] <= a[i]) tail1--;
IM[++tail1] = i;
while( head2 <= tail2 && a[i] <= a[Im[tail2]]) tail2--;
Im[++tail2] = i;
if(head1 <= tail1 && IM[head1] + k <= i ) head1++;
if(head2 <= tail2 && Im[head2] + k <= i ) head2++;
ans1[i] = a[IM[head1]];
ans2[i] = a[Im[head2]];
}
for (int i=k;i<=n;i++) cout<<ans2[i]<<' ';cout<<endl;
for (int i=k;i<=n;i++) cout<<ans1[i]<<' ';cout<<endl;
}
return 0;
}