1911: [Apio2010] Special Action Team time limit:4 Sec Memory limit:64 MB
submit:3478 solved:1586
[Submit] [Status] [Discuss] Descriptioninputoutputsample Input4
-1 10-20
2 2 3 4Sample Output9HINT
SourceSolution
Test instructions is very obvious, the number of n is divided into a number of intervals, so that the total value of the largest, the value of each interval is $powersum=\sum power[i],ans=a*powersum^2+b*powersum+c$
Then the DP transfer equation is obtained: $DP [I]=max (dp[j]+a* (Pos[i]-pos[j]) ^2+b* (Pos[i]-pos[j]) +c) $
So obviously not AC, so consider optimizing the time
Consider the slope optimization, for the transfer to the current position, the optimal solution for $i$, if any $i<j$ to meet the $i$ better then you can get the following:
$ (dp[j]-dp[i]+a* (pos[j]^2-pos[i]^2) +b* (Pos[i]-pos[j])/(2*a* (pos[j]-pos[i)) $ then maintenance can be
Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 1000100intN,a,b,c;intPO[MAXN];Long LongPOS[MAXN],DP[MAXN];intQue[maxn],l,r;Long LongpfLong Longx) {returnx*x;}DoubleSlopeintIintj) { Doublefz=dp[j]-dp[i]+a* (PF (pos[j])-PF (Pos[i]) +b* (pos[i]-Pos[j]); DoubleFm= (2*a* (pos[j]-pos[i])); returnfz/FM;}intMain () {n=read (); A=read (), B=read (), c=read (); for(intI=1; i<=n; i++) Po[i]=read (), pos[i]=pos[i-1]+Po[i]; for(intTmp,i=1; i<=n; i++) { while(L<r && Slope (que[l],que[l+1]) <pos[i]) l++; TMP=Que[l]; Dp[i]=DP[TMP]+A*PF (Pos[i]-pos[tmp]) +b* (pos[i]-pos[tmp]) +C; while(L<r && Slope (que[r-1],que[r]) >slope (que[r],i)) r--; que[++r]=i; } printf ("%lld\n", Dp[n]); return 0; }
Slope optimization good tat.
"BZOJ-1911" Special Ops team DP + slope optimization