一篇很好的单调队列博客:https://www.cnblogs.com/tham/p/8038828.html
用 c++ 提交
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <vector>
#define MAX 1000005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
struct node{
int x,id;
}v[MAX];
int t,n,m;
int a[MAX];
int maxn[MAX];
int minn[MAX];
void getMax(){
int head=1,rear=0,i;
for(i=1;i<m;i++){
while(head<=rear && v[rear].x<=a[i]) //从后往前,小于等于当前a[i]则出队
rear--;
v[++rear].x=a[i];
v[rear].id=i;
}
for(;i<=n;i++){
while(head<=rear && v[rear].x<=a[i]) //从后往前,小于等于当前a[i]则出队
rear--;
while(head<=rear && v[head].id<=i-m) //如果队首不在有效范围内,出队
head++;
v[++rear].x=a[i];
v[rear].id=i;
maxn[i]=v[head].x;
}
}
void getMin(){
int head=1;
int rear=0;
int i;
for(i=1;i<m;i++){
while(head<=rear && v[rear].x>=a[i])
rear--;
v[++rear].x=a[i];
v[rear].id=i;
}
for(;i<=n;i++){
while(head<=rear &&v[rear].x>=a[i])
rear--;
while(head<=rear && v[head].id<=i-m)
head++;
v[++rear].x=a[i];
v[rear].id=i;
minn[i]=v[head].x;
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
getMin();
for(int i=m;i<=n;i++){
if(i==n)printf("%d\n",minn[i]);
else printf("%d ",minn[i]);
}
getMax();
for(int i=m;i<=n;i++){
if(i==n)printf("%d\n",maxn[i]);
else printf("%d ",maxn[i]);
}
}