Problem 8 DP

Source: Internet
Author: User

$des $

$sol $

Kee $f _i$ said to consider the former $i $ A building, and the first $i the height of the building unchanged answer every time
When the transfer is enumerated on the previous building number, the middle section must become the same height, and
Height is less than or equal to the height of both ends.
Assuming a transfer from the $f _j$ and an intermediate height of $t $, then:
$ $f _i = \sum_{k = j + 1} ^ {i-1} (T-h_k) ^ 2 + C (h_j + h_i-2t) $$
Such an intermediate height can be determined $O (1) $ Two for the symmetric axis of the function. Consider optimizing transfers,
Because the middle height is smaller than the two ends, there is at most one $h _j > h_i$ $j $ can be transferred. OK
Maintain a monotonic stack about height so that the effective number of transfers is O (n).

$code $

#include <bits/stdc++.h>usingstd::p air;usingstd::vector;usingSTD::string; typedefLong LongLl;typedef pair<int,int>PII;#defineFST First#defineSND second#definePB (a) push_back (a)#defineMP (A, b) Std::make_pair (A, B)#defineDebug (...) fprintf (stderr, __va_args__)Template<typename t>BOOLChkmax (t& A, T b) {returnA < b? A = B,1:0; } Template<typename t>BOOLChkmin (t& A, T b) {returna > B? A = B,1:0; } Template<typename t> T Read (t&x) {intf =1; x =0; CharCH =GetChar ();  for(;! IsDigit (CH); ch = getchar ())if(ch = ='-') F =-1;  for(; isdigit (ch); ch = getchar ()) x = x *Ten+ CH- -; returnX *=F;}Const intN =1000000;intN, C;intH[n +5];ll s[2][n +5], Dp[n +5];ll Solve (intXintYintMX) {ll a= Y-x-1; LL b= -2* (s[0][y-1]-s[0][X])-(X! =0) * C-(Y! = n+1) *C; ll c= s[1][y-1]-s[1][X] + 1ll * (x! =0) * H[x] * C + 1ll * (Y! = n+1) * H[y] *C;    ll T; T= (ll) ((-B/2/a) +0.5); Chkmax<ll>(t, MX); if(X! =0) Chkmin (T, (LL) h[x]); if(Y <=N) chkmin (T, (LL) h[y]); returnA * t * t + b * t +C;}intMain () {read (n), read (C);  for(inti =1; I <= N; ++i) {read (h[i]); s[0][i] = s[0][i-1] +H[i]; s[1][i] = s[1][i-1] + 1ll * H[i] *H[i]; }    Static intStk[n +5], top; h[0] = H[n +1] = (1<< -); Stk[top++] =0;  for(inti =1; I <= n+1; ++i) {Dp[i]= dp[i-1] + ((i = =1|| i = = n+1) ?0: 1ll * C * std::abs (H[i]-h[i-1]));  while(Top >0&& h[stk[top-1]] <=H[i]) {            if(Top >1) Chkmin (Dp[i], dp[stk[top-2]] + solve (stk[top-2], I, h[stk[top-1]])); --top; } stk[top++] =i; } printf ("%lld\n", Dp[n +1]); return 0;}

Problem 8 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.