Permission problem, no portal.
This is obviously a DP problem, just read the topic may be more confused. If this problem does not require to go back, then is a very naked DP problem. But the first one of the lattice that asked to go back and go back must have been past.
Do not consider the journey back, for a length within the range of $k$, where all values are positive points can be reached. So start with a prefix and:
$sum _i= \sum _{j=1}^i a_j \times [a_j>0]$
After this is done, if it is not returned, the following transfer equations can be obtained:
$f [I]=max \{f[j]+sum[i-1]-sum[j] \}$
In fact, to this step, the state transfer equation with the back is also very obvious.
$f [I]=max \{f[j]+sum[i-2]-sum[j]+a[i]+a[i-1] \}$
Indicates that the $i$ point is the point at which to go and the previous point will be returned, $sum []$ and $f[]$ are monotonic, so the monotonic queue can be used to optimize the decision Monotonicity, so that the overall complexity is reduced to $o (N) $.
But $f[i]_{max}$ is not the final answer, because for any one point $i$,$[i+1,i-1+k]$ can be reached, so the contribution of this paragraph to the answer is also tired plus.
In a specific implementation, note that the monotonic queue should be in line 0 and 1 before DP because the NO. 0 point is not the point at which it must stay.
1 //Bzoj 19152 //by Cydiater3 //2016.10.64#include <iostream>5#include <cstring>6#include <string>7#include <algorithm>8#include <queue>9#include <cstdio>Ten#include <cmath> One#include <ctime> A#include <map> -#include <iomanip> -#include <cstdlib> the using namespacestd; - #definell Long Long - #defineUp (I,j,n) for (int i=j;i<=n;i++) - #defineDown (i,j,n) for (int i=j;i>=n;i--) + Const intmaxn=3e6+5; - Const intoo=0x3f3f3f3f; + inline ll read () { A CharCh=getchar (); ll x=0, f=1; at while(ch>'9'|| ch<'0'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } -ll n,k,sum[maxn],a[maxn],q[maxn],head,tail,f[maxn],ans=0; - namespacesolution{ in voidinit () { -memset (SUM,0,sizeof(sum)); toN=read (); k=read (); +Up (I,1, N) a[i]=read (); -Up (I,1, N) sum[i]=sum[i-1]+ (a[i]>0? A[i]:0); the } * voidDP () { $Head=1; tail=0; q[++tail]=0; q[++tail]=1;Panax NotoginsengUp (I,2, N) { - while(head<tail&&i-q[head]>k) head++; thef[i]=f[q[head]]+sum[i-2]-sum[q[head]]+a[i]+a[i-1]; + while(Head<tail&&f[i]-f[q[tail]]>sum[i]-sum[q[tail]]) tail--; Aq[++tail]=i; the } +Up (I,1, N) Ans=max (ans,f[i]+ ((I-1+k<=n)? (sum[i-1+k]-sum[i]):(sum[n]-Sum[i] )); - } $ voidoutput () { $cout<<ans<<Endl; - } - } the intMain () { - //freopen ("input.in", "R", stdin);Wuyi using namespacesolution; the init (); - DP (); Wu output (); - return 0; About}
View Code
BZOJ1915: [Usaco2010 Open] cow jumping lattice game