CSP-training Week5 Problem B monotone queue problems (POJ - 2823)

Monotone queue problems Week5 Problem B

Architectural Overview

Queue, a common data structure, he simulated the queue data structure our daily lives, and has the characteristics of real life in the queue: first come, first enter the queue, the queue can accept the preceding service. And commonly used as a basic data structure, c ++ STL library also provides a packaged queue structure, can be carried out using the following statement package type of queue

#include<queue>

Information on how the STL queue can be queried c ++ official document Learning: c ++ official document
uses a queue extension data structure in a title - Monotone data structure, and use it to solve the maximum minimum value searching within a region other value Compare issue.

Topic Overview

Topic narrative

ZJM has a length of n number of columns and a size of a window k, the window can be moved back and forth on the columns. ZJM now want to know from left to right in a sliding window, the window each time the maximum value and the minimum value, respectively, What example:
the number of columns is [13-1-35367], where k is equal to 3.

Window State Minimum & maximum
[1 3 -1] -3 5 3 6 7 -1 3
1 [3 -1 -3] 5 3 6 7 -3 3
1 3 [-1 -3 5] 3 6 7 -3 5
1 3 -1 [-3 5 3] 6 7 -3 5
1 3 -1 -3 [5 3 6] 7 3 6
1 3 -1 -3 5 [3 6 7] 3 7

INPUT & SAMPLE INPUT

Enter two lines. The first line of two integers n and k are the size and length of the sliding window series, 1 <= k <= n <= 1000000. The second row has n represents an integer number of columns of ZJM.
Sample input:

8 3
1 3 -1 -3 5 3 6 7

OUTPUT & Sample Output

There are two lines of output. The first output line of each sliding window position from left to right, the minimum value in the sliding window. The second line is the maximum.
Sample output:

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Topic restatement and pit

The problem intended to be abbreviated as outlined the following requirements: a numerical sequence of a given size, and a window, the window to the speed of backward movement step = 1, calculate the maximum value of the state of each window in the window, and minimum.

Problem-solving ideas

If you are familiar monotone queue, this question should be found in the clear meaning of the questions is a straightforward question monotonous board queue, because he has the following a few keywords:
, a sequence value of 1, and sequential access.
2, to search for the maximum value, the minimum value of the range of a partial area (window)
data 3, to be relatively simple process to directly compare the size

Why use a monotone queue:

Overall meaning of the questions shows that the main requirements of the problem is twofold: find the maximum (minimum) value & maintain the size of the window, monotonic behavior we can use a similar monotonous stack solution: maintaining a monotonically increasing (decreasing) sequence, by inserting pop-up method for maintaining an orderly value, the specific content can see: monotonous stack contents Detailed
but another requirement is that we can not meet the existing data structure: Keep the window of the same size.

Monotone specific operation queue

Therefore, we introduced monotone queue (tedious and can be operated from the head, tail)
in claim 1 Solution: maintaining monotonicity, we can maintain the monotone stack on maintaining move over the tail of the queue, the queue can remain maintenance monotone
claim 2 solution: to maintain the value stored in a queue does not exceed the window, you may be used to calibrate the subscript queue, the real storage array subscript of each element in the queue, when present
The tail of the newly inserted element subscript - head of the queue element index> Window Size =
After proof windows mobile, head of the queue element does not belong to the current window, you can pop.
As the queue by maintaining monotonous practice, we can guarantee the team's first queue of the current window is always legitimate (in line with the window size) maximum (minimum), the problem is resolved.

to sum up

A monotonous queue board problem, but may well have to learn monotonous queue central idea by this question:
1, tail maintain monotonicity
2, head to maintain the size of the search area
monotonous monotone queue and stack often mixed the topic, they are resolved and a local search for global search problem in a numerical sequence may be appreciated that the use of contrast.

Source title

#include<iostream>
#include<cstdio>
using namespace std;
int q[1000010];
int a[1000010];
int max_num[1000010];
int min_num[1000010];
int number=0;
int win=0;
void make_max()//维持递减数列
{
    int L=1;
    int R=0;
    for(int i=1;i<=number;i++)
    {
        while(R>=L && a[q[R]]<=a[i])
        {R--;}
        q[++R]=i;
        if(q[R]-q[L]>win-1)
        L++;
        max_num[i]=q[L];
    }
}
void make_min()//维持递增数列数列
{
    int L=1;
    int R=0;
    for(int i=1;i<=number;i++)
    {
        while(R>=L && a[q[R]]>=a[i])
        {R--;}
        q[++R]=i;
        if(q[R]-q[L]>win-1)
        L++;
        min_num[i]=q[L];
    }
}
int main()
{
    scanf("%d %d",&number,&win);
    for(int i=1;i<=number;i++)
    {
        scanf("%d",&a[i]);
    }
    make_max();
    make_min();
    for(int i=win;i<=number;i++)
    {
        printf("%d ",a[min_num[i]]);
    }
    printf("\n");
    for(int i=win;i<=number;i++)
    {
        printf("%d ",a[max_num[i]]);
    }
    printf("\n");
    
    return 0;
}
Published 17 original articles · won praise 2 · Views 1656

Guess you like

Origin blog.csdn.net/qq_43942251/article/details/105160021