"Bzoj" "3156" defense readiness

Source: Internet
Author: User

dp/slope optimization

Slope optimization of the bare question ...

Sigh ... And the $10^6$ as 10W ... Re has n-fat

The problem is still very water.

Of course, in reverse order can also do ... But the whole thing turned out better.

Reversal of the a[0]= before the reversal of the a[n], and so on until A[n-1], inverted a[n]=0;

F[0]=a[0], because the first guard tower must be selected in the initial state.

Then define F[I] to show the guard tower at position I, 0~i the minimum value of the cost

Easy to get $f[i]=min\{f[j]+\frac{(i-j) * (i-j-1)}{2}+a[i] \} $

Monotonic Proof: $ (J > K) $

\[\begin{aligned} f[j]-f[k]+\frac{(i-j) * (i-j-1)}{2}-\frac{(i-k) * (i-k-1)}{2} &< 0 \ \ f[j]-f[k]+\frac{j^2+j-k^ 2-K}{2} &< i* (j-k) \ \frac{f[j]-f[k]+\frac{j^2+j-k^2-k}{2}}{j-k} &< i \end{aligned} \]

1 /**************************************************************2 problem:31563 User:tunix4 language:c++5 result:accepted6 time:2476 Ms7 memory:24708 KB8 ****************************************************************/9  Ten //Bzoj 3156 One#include <cstdio> A#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) the #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back - using namespacestd; + intGetint () { -     intv=0, sign=1;CharCh=GetChar (); +      while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} A      while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} at     returnv*Sign ; - } - Const intn=1000010; -typedefLong LongLL; - /******************tamplate*********************/ - LL n,q[n],l,r; in LL A[n],f[n]; -InlineDoubleSlop (LL k,ll j) { to     return Double(f[j]-f[k]+ (J*J+J-K*K-K)/2.0)/Double(J-k); + } - intMain () { then=getint (); *D (i,n-1,0) a[i]=getint (); $f[0]=a[0];Panax NotoginsengF (I,1, N) { -          while(L<r && Slop (q[l],q[l+1]) <i) l++; the         intt=Q[l]; +f[i]=f[t]+ (LL) (i-t) * (i-t-1)/2+A[i]; A          while(L<r && Slop (q[r-1],q[r]) >slop (q[r],i)) r--; theq[++r]=i; +     } -printf"%lld\n", F[n]); $     return 0; $}
View Code

"Bzoj" "3156" defense readiness

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.