[Luo valley P1440] m for the minimum in the interval - Monotone queue

emm konjac recently learned monotonous queue, which is a monotonous queue entry title, and then have some solution to a problem is to speak directly to the code, konjac talk about themselvesEasy to understandThe idea of ​​it. (In fact, the code is very short, only five core line, you do not have to panic)

Violence miracle - try \ (O (n * m) \) violence

This is a problem for all to see first reaction surface problem, certainly violence. Before each find \ (m \) number is not on the list? A look at the data range, suddenly silent. \ (O (n * m) \) violence perfect \ (TLE \) .

Find the optimum opportunity - looking for repeat

We observed sample, and found some rules - looking for \ (i \) before \ (m \) number, I found a duplicate. We are looking \ (i-1 \) before \ (m \) number when enumerating interval \ ([IM,. 1-I] \) . And we are looking for \ (I \) before \ (m \) number of times, and enumerating interval \ ([I-m +. 1, I] \) . That is, we repeat enumerate the \ (m-1 \) number, which led to such a high number of time complexity.

Figure (pure painting mouse, no tragedy touch screen).

Discover the essence of the problem - monotonous queue

We found that, in the evaluation minprocess values of repeated traversal of the array, resulting in low efficiency.

Well, we can not record the minvalue of it? We found that, in fact, we requested interval is fixed . Then we think about,

If, \ (J \) at an interval which is a minimum value, at this time, to a \ (I \) . \ (i \) than \ (j \) to smaller, than \ (j \) and later (in other words, \ (i \) than \ (j \) young off than \ (j \) Excellent) , then \ (j \) interval after which never become \ (min \) value of! Well, we can not consider \ (j \) , the \ (j \) pop.

Well, we found that this is a FIFO data structure, it is clear that we are using queue to operate.

However, we also need to consider one thing: the queue of each element should be better than before, so we also need to maintain monotonic queue.

We introduced a new data structure: monotonous queue \ (Monotone-Queue \)

This data structure is often associated with a dynamic programming algorithm is used in conjunction acting state 1D / 1D transition like dynamic programming. In this question, the we maintain monotonicity at one end and the other end to maintain timeliness .

Code

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int a[2000005], que[2000005];
// que数组模拟单调队列的操作

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    int head = 1, tail = 0;
    printf("0\n");
    // 一开始,先输出一个0
    for (int i = 1; i <= n-1; i++) {
    while (head <= tail && a[que[tail]] >= a[i]) tail--;
    // 维护单调性
    que[++tail] = i;
    // 入队
    while (head <= tail && que[head] <= i-m) head++;
    // 维护时效性
    printf("%d\n", a[que[head]]);
    // 最后输出
    } 
    return 0;
} 

summary

In summary, the application of monotonous queue OI little. However, this is a very important board. If you understand thoroughly, and put it memorized. Many types of dynamic programming optimization is based on this template. In addition, konjac just learning OI, dish to not work. So, please Gangster exhibitions.

Guess you like

Origin www.cnblogs.com/ByhBlog/p/11416332.html