The maximum minimum value of k is sliding, sliding one at a time
Maintaining possible answer values with a double-ended queue
Maintain a monotonically increasing sequence if minimum values are required
To the beginning of the first K, the newly added if smaller than the end of the team, then the tail of the team, until the new join the team tail, join the tail
From K+1 to the last one, press in the new number according to the above rules, and then eject the first element of the team (the position that satisfies the original sequence of the first element of the team must be in the window, otherwise, continue to pop the next)
#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include < Cstring> #include <cmath>using namespace std;inline void put (int x) {if (x< 0) {putchar ('-'); x =-X; } if (x = = 0) {Putchar (' 0 '); Return } Char s[20]; int bas = 0; for (; x;x/=10) s[bas++] = x%10+ ' 0 '; for (; bas--;) Putchar (S[bas]); return;} int n,k;int num[1111111];int ansmin[1111111];int pj;int ansmax[1111111];int pjj;struct node{int p,v;} Q[1111111];int Main () {#ifndef online_judgefreopen ("G:/1.txt", "R", stdin); Freopen ("G:/2.txt", "w", stdout); # ENDIFSCANF ("%d%d", &n,&k), for (int i=1;i<=n;i++) {scanf ("%d", &num[i]);} int start=1,tail=1;for (int i=1;i<=n;i++) {if (i==1) {q[1].v=num[1]; Q[1].p=1; }//for (int j=tail;j>=start;j--)//eject larger number than it//{//if (Q[j].v>num[i])//tail--;//}while (tail>=start&&q [Tail].v>num[i]) tail--;q[++tail].v=num[i];q[tail].p=i;Add this number to the tail//Start output minimum if (i>=k) {while (i-q[start].p>k-1) start++;ansmin[pj++]=q[start].v;}} start=1;tail=1;for (int i=1;i<=n;i++) {if (i==1) {q[1].v=num[1]; Q[1].p=1; }//for (int j=tail;j>=start;j--)//eject a number smaller than it//{//if (Q[j].v<num[i])//tail--;//}while (tail>=start&&q [Tail].v<num[i]) tail--;q[++tail].v=num[i];q[tail].p=i;//adds the number to the tail//Start output minimum if (i>=k) {while (i-q[start].p>k -1) start++;ansmax[pjj++]=q[start].v;}} for (int i=0;i<pj;i++) {put (ansmin[i]);//printf ("%d%c", ansmin[i],i==pj-1? ' \ n ': ');p Utchar (');} Putchar (' \ n '); for (int i=0;i<pjj;i++) {put (ansmax[i]);//printf ("%d%c", ansmax[i],i==pjj-1? ' \ n ': ');p Utchar (');} Putchar (' \ n ');}