https://www.acwing.com/problem/content/156/
#include <the iostream> the using namespace STD; const int N = 1000010 ; int A [N], q [N]; // A is the original value, q is the queue is the index stored int main () { int n-, K; Scanf ( " % D% D " , & n-, & K); for ( int I = 0 ; I <n-; I ++) Scanf ( " % D " , & A [I]); int HH = 0 , = TT - . 1 ; // define the tail and head of line, initially empty for ( int I =0 ; I <n-; I ++) { // I is a right end point // as the maximum number is not only a window, so that only a movement, i.e. with IF // determines whether queue head overshot window, the first judgment is not empty IF (hh <&& TT = I - K + . 1 > q [hh]) // end is i, the starting point is the i-k + 1, q if the starting point is greater than [hh], have been described slipping window HH ++; // after the team plus a header, to be, before the number can no longer be used in the while (HH <&& a TT = [Q [TT]]> = a [I]) TT - -; // if ready to insert numbers smaller than the tail, then the tail of no use, then put the team deleted tail, it would have been removed until, not satisfied hh <= tt, queue becomes empty // although it will be deleted, but will not change the length of the sliding window, because the sliding window is moved according to the index // if intended for insertion figure than the tail of a large team, do not delete, insert directly, eventually monotonous incrementing q [++ tt] = i;// then current digital insert, into the tail, if before deleting the queue becomes empty, because +1, it will be a multi- IF (i> = k - 1 ) printf ( " % d " , a [Q [HH]]); // as the team head will be minimal, so that the output queue head } the puts ( "" ); HH = 0 , TT = - . 1 ; for ( int I = 0 ; I < n-; I ++ ) { IF (HH <&& TT = I - K + . 1 > Q [HH]) HH ++ ; the while (HH <&& A TT = [Q [TT]] <= A [I]) TT - ; Q [ ++ TT] = I; IF (i >= k - 1) printf("%d ", a[q[hh]]); } puts(""); return 0; }