Optimization of the queue and monotone] [dp

I probably looked drab queue optimized for DP, so write this article, hope there is help.

(Dp or push equation difficulty qwq)

Example 1.

Title effect: the sequence number n, the number selected such that it does not exceed the number of successive k, and the maximum and.

Equation relatively good push this question: provided dp [i] [0/1] as to the i-th and i-th not selected / selected maximum value.

The metastasis: dp [i] [0] = max (dp [i-1] [0], dp [i-1] [1])

dp[i][1]=max{dp[j][0]-sum[j]+sum[i]},i-k<=j<i

J can enumerate.

But the title would be so let your water before?

Find time out to optimize the inevitable.

Careful observation equation, considering its specificity.

For equation 1, we do not intend to do anything more. But for Equation 2, it is clear there is still room for optimization.

We will sum [i] proposed to give:

dp[i][1]=max{dp[j][0]-sum[j]}+sum[i].

This equation is only related with the j, we like to let the largest max inside.

Maintain it, we can use the heap, you can also use monotonous queue, tree line.

This article is mainly about the optimization monotonous queue. It can ensure that the time complexity of O (n) a.

First of all, we are clear that we maintain the largest team first element. Obviously, the number in the queue to be monotonically decreasing.

Second, we must strictly ensure that the range of our inquiry, to ensure there are no queues digital useless.

And each enumeration to the next number, pay attention to the update queue.

When to consider updating the better:

First, when this number is not required when the range, can be deleted.

Secondly, the newly inserted digital, we need to insert the tail, which compares the value of better:

Determine their "wastage" button.

With Q [] indicates that the queue, S [] represents a prefix and, it is determined that:

s[q[head]]-f[q[head]][0]>s[i]-f[i][0]&&head<=tail

If they do, out of it, delete it. Because the multi-element tail team wasted than the newly inserted value, it is clearly not as good as certain advantages.

As a result, we have to ensure the stability of the complexity of the monotony of the queue.

Given the code:

 

#include<cstdio>
#include<iostream>
using namespace std;
long long q[2000000],f[2000000][2];
long long n,k,s[2000000],a[2000000];
long long tail,head;
int main(){
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;++i){
        scanf("%lld",&a[i]);
        S [I] = S [I- . 1 ] + A [I]; // SUM 
    } 
    tail = = head . 1 ; // initialization 
    for ( int I = . 1 ; I <= n-; ++ I) { 
        F [I ] [ 0 ] = max (F [I- . 1 ] [ 0 ], F [I- . 1 ] [ . 1 ]); // for not selected i, only the first two can be considered 
        the while (Q [head] <&& head IK <= tail) head ++; // determines whether the head-of-looking interval 
        F [I] [ . 1 ] = F [Q [head]] [ 0 ] -s [Q [] head] + S [I]; / / taking MAX transfer 
        the while (F [I] [ 0] -s [I]> F [Q [tail]] [ 0 ] -s [Q [tail]] && head <= tail) tail-- ; 
        Q [ ++ tail] = I; // update the tail, when several current queue and the tail is not satisfied if i is inserted monotonic
         // written as s [i] -f [i] [0] <s [q [tail]] - f [q [tail]] [0] may be
         // will be understood and is selected from the tail to i, are not both selected from cows and efficiency compared with apparently less waste and more preferably, not preferable to delete 
    } the printf ( " % LLD \ n- " , max ( F [n-] [ 0 ], F [n-] [ . 1 ]));
     return  0 ; 
}

 

Double experience: P2034

Continuous update.

 

Guess you like

Origin www.cnblogs.com/h-lka/p/11220841.html