$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