AcWing 154. 滑动窗口
原题链接
简单
作者:
wangyj
,
2020-10-01 20:38:38
,
所有人可见
,
阅读 380
#include<cstdio>
using namespace std;
int a[1000005],b[1000005];
int main()
{
int n,k,i,h=0,t=-1;
scanf("%d%d",&n,&k);
for(i=0;i<n;i++)scanf("%d",&a[i]);
for(i=0;i<n;i++){
if(h<=t&&i-k+1>b[h])h++;
while(h<=t&&a[b[t]]>=a[i])t--;
t++,b[t]=i;
if(i>=k-1)printf("%d ",a[b[h]]);
}
printf("\n");
h=0,t=-1;
for(i=0;i<n;i++){
if(h<=t&&i-k+1>b[h])h++;
while(h<=t&&a[b[t]]<=a[i])t--;
t++,b[t]=i;
if(i>=k-1)printf("%d ",a[b[h]]);
}
printf("\n");
return 0;
}