&& P1886 sliding window queue optimization problem solution monotone

Monotonous queue:

As the name suggests, is a queue element is monotonic (or single down monocytogenes).

In some problems to optimize complexity.

Dp in question, there is a thematic monotony queue optimization of dynamic programming, the future will be updated ( now or in the not too dishes ).

When you see a similar problem is similar to the slide of a given length section may be considered monotonous queue optimization.

Like this question: P1886 sliding window.

 

A template question. Then we start to explain the problem.

 


First of all, you read the title. . (??)

1. Violence

The idea is simple, entry-level dual circulation for enumeration in the range of each element of the current and take min and max.

Because the inner layer for enumeration head section, the inner section for enumeration position, the complexity of O (inner layer interval length interval length *) i.e. O (nk). This problem is data 1e6 ah. . T fly. .

2. ST table

Students learned more advanced data structures might think of tree line and st table.

Among them, the segment tree can be O (nlogn) query, O (nlogn) changes. st table only support queries, but O (nlogn) pretreatment, in this question, there is no modification of the operation, so we can use st table, relative to the tree line better.

But the calculation, the actual complexity of the twenty million? . . It may be too, but I did not dare try . . . qwq. General complexity of tens of millions had to see the face before. .

So there is a more stable algorithm:

3. monotone queue

Complexity of O (n), a linear algorithm.

Protagonist coming!

We define a deque q, any of which both sides can be shortened to increase the length, we try to maintain its length to be k.

When this window is moved one space to the right, natural elements to get into a team, a team element.

We try to ensure that the team is the largest element (for example sub) value of the current range, then in order to maintain the monotony of the queue, we must make the element behind the element is larger than the head of the team.

Consider a oi saying: some people younger than you, he is stronger than you, then you can retire ( sympathetically )!

For the current to enter the team ah a [i], if you set up a j, to ensure a [J] in the tangential force a [J] than a [i] to enqueue (head <j <= tail), if a [j] is currently a [i] little (stronger than you) ratio, also advanced team (than you), is not on when the maximum opportunity to rub out a [j]?

Then a [j], and those, like a [j] for a class of elements (al) can be ejected queue (collectively retired) a. .

Why read, read inexplicable bitterness. . It died

It is also an important foundation, you will want to know the truth!

So he transformed into a code like this:

Pop the tail.

So, as long as the output of each team heads (class of rk1) on it.

code show as below:

#include<cstdio>
using namespace std;
int read()
{
    char ch=getchar(),last=' ';
    int ans=0;
    while(ch>'9'||ch<'0')last=ch,ch=getchar();
    while(ch>='0'&&ch<='9')ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return last=='-'?-ans:ans;
}

int n,a[1000001],k,q[1000001];

inline int minnn()//求最小值
{
    int hea=0,tai=1;
    for(int i=1;i<=n;i++)
    {
        while(hea<=tai&&q[hea]+k<=i)hea++;
        while(hea<=tai&&a[i]<a[q[tai]])tai--;//又小又强
        q[++tai]=i;
        if(i>=k)printf("%d ",a[q[hea]]);//输出rk1
    }
    printf("\n");
}

inline int maxnn()//求最大值
{
    int hea=0,tai=1;
    for(int i=1;i<=n;i++)
    {
        while(hea<=tai&&q[hea]+k<=i)hea++;
        while(hea<=tai&&a[i]>a[q[tai]])tai--;//又小又强
        q[++tai]=i;
        if(i>=k)printf("%d ",a[q[hea]]);//输出rk1
    }
    printf("\n");
}

int main(){
    n=read(),k=read();
    for(int i=1;i<=n;i++)
    {
        a[i]=read();
    }
    minnn();
    maxnn();
    return 0;
}

A poignant explanation.

end.

 

I do not want to retire

 

Guess you like

Origin www.cnblogs.com/lbssxz/p/11361613.html