Bzoj 3156 Defense Readiness (slope DP)

Source: Internet
Author: User

3156: Defense Readiness Time Limit:10 SEC Memory limit:512 MB
submit:837 solved:395
[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 Input

10
2 3 1 5 4 5 6 3 1 2

Sample Output
18

HINT



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

Source

katharon+ #1

Ideas

Slope optimization DP.

The only thing to note is that the data over-range in the process needs to be specifically stated.

for the gross slope dp both long <_<

"Code 1"

1#include <cstdio>2#include <iostream>3 using namespacestd;4 5typedefLong LongLL;6 Const intN = 1e6+Ten;7 structPoint {LL x, y;8 }q[n],now;9 intN,l,r,a[n];Ten  One LL Cross (Point a,point b,point c) { A     return(b.x-a.x) * (C.Y-A.Y)-(B.Y-A.Y) * (c.x-a.x); - } - voidReadint&x) { the     CharC=getchar (); while(!isdigit (c)) c=GetChar (); -x=0; while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar (); - } - intMain () { +     //freopen ("in.in", "R", stdin); -     //freopen ("Out.out", "w", stdout); + read (n); A      for(intI=1; i<=n;i++) read (A[i]); at      for(intI=1; i<=n;i++) { -          while(L<r && q[l].y-q[l].x*i >= q[l+1].y-q[l+1].x*i) l++; -now.x=i; -now.y=q[l].y-(LL) q[l].x*i+ (LL) i*i+A[i]; -          while(L<r && Cross (q[r-1],q[r],now) <=0) r--; -q[++r]=Now ; in     } -printf"%lld", q[r].y-(LL) n (n+1)/2);//(LL) n (n+1)/2 to     return 0; +}
Folding 1

"Code 2"

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6typedefLong LongLL;7 Const intN = 1e6+Ten;8 9 intN,l,r,a[n],q[n]; LL F[n]; Ten  One DoubleSlopintKintj) { A     return(Double) (f[j]-f[k]+ (LL) j* (j+1)/2-(LL) k* (k +1)/2)/(J-k); - } - voidReadint&x) { the     CharC=getchar (); while(!isdigit (c)) c=GetChar (); -x=0; while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar (); - } - intMain () { +     //freopen ("in.in", "R", stdin); -     //freopen ("Out.out", "w", stdout); + read (n); A      for(intI=1; i<=n;i++) read (A[i]); at      for(intI=1; i<=n;i++) { -          while(L<r && Slop (q[l],q[l+1]) <i) l++; -         intt=Q[l]; -f[i]=f[t]+ (LL) i* (I-T) + (LL) t* (t+1)/2-(LL) i* (i+1)/2+A[i]; -          while(L<r && Slop (q[r-1],q[r]) >slop (q[r],i)) r--; -q[++r]=i; in     } -printf"%lld", F[n]); to     return 0; +}
Folding 2

Bzoj 3156 Defense Readiness (slope DP)

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.