"BZOJ-3156" Defense preparation DP + slope optimization

Source: Internet
Author: User

3156: Defense Readiness Time Limit:10 SEC Memory limit:512 MB
submit:951 solved:446
[Submit] [Status] [Discuss] Description Input

The first behavior of an integer n indicates the total length of the front.

The second row of n integers, and the first integer represents the cost of placing the guard tower in position I ai.

Output

A total of one integer that represents the minimum front cost value.

Sample Input10
2 3 1 5 4 5 6 3 1 2
Sample Output18
HINT

1<=n<=10^6,1<=ai<=10^9

Source

katharon+ #1

Solution

Slope optimization DP

The plan is shifted from behind? Flip the sequence.

Then there is the transfer equation $DP [I]=min (dp[i],dp[j]+sum[i-1]-sum[j]-(i-j-1) *j+a[i]$

Then the slope is optimized, the comparison is bare, and then you can

Notable places:

Transfer, $i =1$ in advance to deal with it.

When calculating the answer, note that a long long or else will wa into a dog.

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>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[MAXN];intQue[maxn],l,r;Long LongDp[maxn],sumid[maxn],ans;DoubleSlopeLong LongILong Longj) {return(Double) (DP[I]-DP[J]-SUMID[I]+SUMID[J]+I-J+I*I-J*J)/(Double) (I-j);}intMain () {n=read (); Ans= (Long Long)1<< -;  for(intJ=m; i>=1; i--) a[i]=read ();  for(intI=1; i<=n; i++) sumid[i]=sumid[i-1]+i; dp[1]=a[1]; que[1]=1; L=r=1;  for(intTmp,i=2; i<=n; i++)        {             while(L<r && Slope (que[l],que[l+1]) <i) l++; TMP=Que[l]; Dp[i]=dp[tmp]+sumid[i-1]-sumid[tmp]-(i-tmp-1) *tmp+A[i];  while(L<r && Slope (que[r],i) <slope (que[r-1],QUE[R]) r--; que[++r]=i; }     for(intI=1; i<=n; i++) Ans=min (ans,dp[i]+sumid[n]-sumid[i]-(Long Long) (n-i) *i); printf ("%lld\n", ans); return 0;}

The above mentioned WA into a dog ... Mdzz

"BZOJ-3156" Defense preparation 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.