P1886 sliding window, p1886 Sliding Window
Description
There are now a bunch of numbers with N numbers (N <= 10 ^ 6) and a window in k size. Now this slide from the left to the right, sliding one unit each time, find the maximum and minimum values in the window after each sliding.
For example:
The array is [1 3-1-3 5 3 6 7], and k = 3.
Input/Output Format
Input Format:
There are two rows in the input. The first row is n, k.
Number of the second behavior n (<INT_MAX ).
Output Format:
The output contains two rows. The first row is the minimum value of every window sliding.
The second action is the maximum sliding value of each window.
Input and Output sample
Input example #1:
8 31 3 -1 -3 5 3 6 7
Output sample #1:
-1 -3 -3 -3 3 33 3 5 5 6 7
Description
50% of data, n <= 10 ^ 5
100% of data, n <= 10 ^ 6
Maximum and minimum maintenance for monotonous queues
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=10000001; 7 int read(int & n) 8 { 9 char c='.';int x=0,flag=0;10 while(c<'0'||c>'9')11 {12 c=getchar();13 if(c=='-')flag=1;14 }15 while(c>='0'&&c<='9')16 {17 x=x*10+(c-48);18 c=getchar();19 }20 if(flag==1)n=-x;21 else n=x;22 }23 int n,m;24 int a[MAXN];25 int q[MAXN],p[MAXN],h=0,t=0;26 void find_min()27 {28 h=1;t=0;29 for(int i=1;i<=n;++i)30 {31 32 while(h<=t&&q[t]>=a[i])33 t--;34 q[++t]=a[i];35 p[t]=i;36 while(p[h]<=i-m)37 h++;38 if(i>=m)39 printf("%d ",q[h]);40 }41 printf("\n");42 }43 void find_max()44 {45 h=1;t=0;46 memset(q,0,sizeof(q));47 memset(p,0,sizeof(p));48 for(int i=1;i<=n;++i)49 {50 while(h<=t&&q[t]<=a[i])51 t--;52 q[++t]=a[i];53 p[t]=i;54 while(p[h]<=i-m)55 h++;56 if(i>=m)57 printf("%d ",q[h]);58 }59 printf("\n");60 }61 int main()62 {63 read(n);read(m);64 for(int i=1;i<=n;i++)65 read(a[i]);66 find_min();67 find_max();68 return 0;69 }