Luogu P1886 Sliding Window (Monotone Queue)

Keywords: PHP less

Um...

 

Topic link: https://www.luogu.org/problem/P1886

 

Firstly, this problem is a typical template problem for standard monotonic queues (some people say monotonic queues can only solve this problem). This problem can be handwritten in a queue, or can be used in STL double-ended queue.

Core idea: If a person is stronger and smaller than you, then you can't surpass him.

 

We separate the maximum and minimum values of the interval. Here we explain the maximum and the minimum, and vice versa.

If there is an element in the queue and it is larger than the element to be inserted, that is, the person is stronger than you and smaller than you, then you can never be stronger than him, so let you out and let that person join the queue. Then deal with the "retired" person, if the person was in the queue, but not in the current window, then also pop it out from the head of the team.

Finally, note that the initialization queue is empty and the output is empty.

 

AC Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 const int maxn = 1e6 + 5;
 6 int n, k, head, tail, a[maxn];
 7 
 8 struct node{
 9     int id, val;
10 } q[maxn];
11 
12 inline void work_min(){
13     head = 1; tail = 0;
14     for(int i = 1; i <= n; i++){
15         while(head <= tail && a[i] <= q[tail].val) tail--;
16         q[++tail].id = i;
17         q[tail].val = a[i];
18         while(q[head].id <= i - k) head++;
19         if(i >= k) printf("%d ", q[head].val);
20     }
21     printf("\n");
22 }
23 
24 inline void work_max(){
25     head = 1; tail = 0;//Initialization 
26     for(int i = 1; i <= n; i++){
27         while(head <= tail && a[i] >= q[tail].val) tail--;//Less than you and better than you 
28         q[++tail].id = i;
29         q[tail].val = a[i];
30         while(q[head].id <= i - k) head++;//retire 
31         if(i >= k) printf("%d ", q[head].val);//output 
32     }
33     printf("\n");
34 }
35 
36 int main(){
37     scanf("%d%d", &n, &k);
38     for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
39     work_min();
40     work_max();
41     return 0;
42 }
AC code

Posted by nade93 on Fri, 11 Oct 2019 12:06:39 -0700