1. Title Description: Click to open the link
2. Solving ideas: The problem is solved by using monotone queue. Like monotone queues and monotonic stacks, internal elements are arranged in a strictly monotone increments. A typical application of a monotone queue is the problem of finding the most value of a sliding window. So how to solve it? First of all, because of the length of K, we can first put 0 to k-1 subscript all try to enter the queue. When element i is added, if the value J at the end of the queue satisfies Aj≥ai, it is continuously fetched until the queue is empty or Aj<ai and the end is joined by I. At this point, if K-1 is also added to the queue, look at the value of the queue header, J, then B0=aj. If the queue header element ==i-k+1, then this element will certainly not be used, delete. This will find the minimum value. Use the same similar notation to find the maximum value.
It is also easy to understand that the minimum value can be calculated from the beginning to the end, and the maximum value can be obtained from the end of the head slip.
3. Code:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <algorithm># include<cassert> #include <string> #include <sstream> #include <set> #include <vector># include<stack> #include <map> #include <queue> #include <deque> #include <cstdlib># include<cstdio> #include <cstring> #include <cmath> #include <ctime> #include <cctype># include<functional>using namespace std; #define ME (s) memset (s,0,sizeof (s)) typedef long Long Ll;typedef unsigned int uint;typedef unsigned long long ull;typedef pair <int, int> p;const int n=1000000+10;int n,k;int a[n];int b[N];i NT C[n];int deq[n];int Main () {while (~scanf ("%d%d", &n,&k) && (n| | k) {for (int i=0;i<n;i++) scanf ("%d", &a[i]); int s=0,t=0; The minimum value for (int. i=0;i<n;i++) {while (s<t&&a[deq[t-1]]>=a[i]) t--;//does not meet the strict monotonic increment, constantly Remove deq[t++]=I From the tail into the queue if (i-k+1>=0) b[i-k+1]=a[deq[s]]; If the starting point of the sliding window becomes non-negative, then the minimum value is A[deq[s]] if (deq[s]==i-k+1) s++; Note that the starting point of the sliding window and the same team head, the future sliding window will not be used to the first element of the team, delete} s=t=n; Use the same method to find the maximum value, except for the reverse order (int i=n-1;i>=0;i--) {while (s<t&&a[i]>=a[deq[s]) s++; Deq[--s]=i; Insert element from head if (i+k-1<n) c[i]=a[deq[t-1]]; If the end of the sliding window is <n, then the maximum value equals a[deq[t-1]] if (deq[t-1]==i+k-1) t--; If the end of the sliding window is the same as the tail of the queue, the subsequent sliding window will not be used for the end of the queue, delete} for (int i=0;i<=n-k;i++) printf ("%d%c", b[i], "\ n" [I==n-k] ); for (int i=0;i<=n-k;i++) printf ("%d%c", c[i], "\ n" [i==n-k]); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2823 Sliding Window