Title Description
Now there are a bunch of numbers with N numbers (n<=10^6), and a window of size K. Now this swipe from the left to the right, sliding one unit at a time to find the maximum and minimum values in the window after each slide.
For example:
The array is [1 3-1-3 5 3 6 7], and k = 3.
input/output format
Input format:
Enter a total of two lines, the first behavior n,k.
The second behavior n number (<int_max).
Output format:
Output a total of two lines, the first behavior of each window sliding minimum value
Second behavior the maximum value of each window slide
input/Output sampleInput Sample
8 31 3-1-3 5 3 6 7
Output Sample
-1-3-3-3 3 33 3 5 5 6 7
Description
50% of data, n<=10^5
100% of data, n<=10^6
Ideas:
A template, thought very well understand, I think difficult in judging interval, super you have to head++, not monotonous you have to tail--
Code
#include <stdio.h>#include<algorithm>using namespacestd;Const intmx=1e6+1;intN,K,P[MX],Q[MX],A[MX];voidGetmax () {intHead=1, tail=0; for(intI=1; i<=n;++i) { while(Head<=tail && q[tail]<=a[i]) tail--; q[++tail]=A[i]; P[tail]=i; while(p[head]<=i-k) head++; if(i>=k) printf ("%d", Q[head]); }}voidgetmin () {intHead=1, tail=0; for(intI=1; i<=n;++i) { while(Head<=tail && q[tail]>=a[i]) tail--; q[++tail]=A[i]; P[tail]=i; while(p[head]<=i-k) head++; if(i>=k) printf ("%d", Q[head]); } printf ("\ n");}intMain () {scanf ("%d%d",&n,&k); for(intI=1; i<=n;++i) {scanf ("%d",&A[i]); } getmin (); Getmax (); return 0;}/*8 to 3-1-3 5 3 6 7*/
Sliding window (Luogu 1886)