better_Z @ 2023-06-29 21:31:16
#include<iostream>
#include<cmath>
#define maxn 100010
using namespace std;
int n,k;
int a[maxn],q[maxn];
void max(){
int head=0,tail=0;
for(int i=1;i<=n;i++){
while(head<tail&&q[head]+k<=i)head++;
while(head<tail&&a[q[tail-1]]<a[i])tail--;
q[tail]=i;
tail++;
if(i>=k)cout<<a[q[head]]<<" ";
}
}
void min(){
int head=0,tail=0;
for(int i=1;i<=n;i++){
while(head<tail&&q[head]+k<=i)head++;
while(head<tail&&a[q[tail-1]]>a[i])tail--;
q[tail]=i;
tail++;
if(i>=k)cout<<a[q[head]]<<" ";
}
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
min();
cout<<endl;
max();
return 0;
}
by Jerryfish @ 2023-06-29 22:02:10
@better_Z 你这头像。。。
by technopolis_2085 @ 2023-06-29 22:02:20
@better_Z 数组开小了,题目要求