Novo Lu Qi deque optimization transition equation

As we all know, Qi Lu promise is to fool the famous fairy ice.

Topic: https://www.luogu.org/problemnew/show/P1725

 

Clearly, the DP is a problem (apparently the word is disgusting, disgusting to you now)

 

State transition is dp [i] = max (dp [k]) + a [i];

The current position is the maximum jump can jump to the front of all the positions where the maximum value of the current position plus frozen;

Why deque it (because it was great)

This has been a very naked, thinking sliding window. T DP is pure will, so we want to optimize the time to find the maximum value;

If the current value is greater than the value of the queue, the queue has been put elements of pop (because we are traversing the front to back, a position in front and the value of the smaller elements will not make any contributions);

Code

#include<cstdio>
#include<cstring>
#include<deque>
#include<algorithm>
using namespace std;
const int maxn=200100;
struct node
{
    int val;//DP最大值 
    int num;//
};

int n,a[maxn],l,r;
int dp[maxn];
deque <node> s;
int main()
{
    scanf("%d%d%d",&n,&l,&r);
    for(int i=0 ; I <= n-; I ++ ) 
    { 
        Scanf ( " % D " , A + I); 
    } 
    int tail = 0 ; DP [tail] = 0 ; 
    Node X; 
    for ( int I = L; I <= n-; I ++ ) // starting from 0 at the transition to l 
    {
         the while (s.size () && DP [tail]> = s.back () Val). 
        { 
            s.pop_back (); 
        } // useless value pop 
        x.num = tail ; x.val = DP [tail]; 
        s.push_back (X); // add a new value 
        IF (tail-s.front () NUM> = (L-R & lt +. . 1)) S.pop_front (); // queue length 
        DP [i] = s.front () Val + A [i];. // At this point the head of the queue i is the maximum front to reach 
        tail ++ ; 
    } 
    int ANS = - MAXN;
     for ( int I = n--R & lt + . 1 ; I <= n-; I ++ ) 
    { 
        ANS = max (ANS, DP [I]); 
    } 
    the printf ( " % D " , ANS);
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/WHFF521/p/10960285.html
lu