"BZOJ-1911" Special Ops team DP + slope optimization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.