单调队列
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4;
struct data{
int id;
int value;
}q[maxn];
int a[maxn],n,k,l,r;
int main(){
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
l=0,r=-1;
for(int i=0;i<n;i++){
while(l<=r&&q[l].id<=i-k) l++;
while(l<=r&&q[r].value<=a[i]) r--;
q[++r]={i,a[i]};
printf("%d %d\n",i,q[l].value);
}
}
/*
10 3
8 7 12 5 16 9 17 2 4 6
*/
单调栈
for(int i=1;i<=n;i++)
L[i]=R[i]=i;
for(int i=2;i<=n;i++){
int now=i;
while(now>1&&a[i]<=a[now-1]) now=L[now-1];
L[i]=now;
}
for(int i=n-1;i;i--){
int now=i;
while(now<n&&a[i]<=a[now+1]) now=R[now+1];
R[i]=now;
}