$des$

$sol$

记 $f_i$ 表示考虑前 $i$ 个建筑, 并且第 $i$ 个建筑的高度不变的答案, 每次
转移时枚举上一个不变的建筑编号, 中间的一段一定变成相同的高度, 并且
高度小于等于两端的高度.
假设从 $f_j$ 转移且中间高度为 $t$, 则:
$$f_i = \sum_{k = j + 1} ^ {i - 1} (t - h_k) ^ 2 + c(h_j + h_i - 2t)$$
这样中间的高度可以 $O(1)$ 求二次函数的对称轴确定. 考虑优化转移,
因为中间高度要小于两端, 所以最多只有一个 $h_j > h_i$ 的 $j$ 能够转移. 可以
维护关于高度的单调栈, 这样有效的转移次数就是 O(n) 的.

$code$

#include <bits/stdc++.h>

using std::pair;
using std::vector;
using std::string; typedef long long ll;
typedef pair<int, int> pii; #define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__) template <typename T> bool chkmax(T& a, T b) { return a < b ? a = b, : ; }
template <typename T> bool chkmin(T& a, T b) { return a > b ? a = b, : ; } template <typename T> T read(T& x) {
int f = ; x = ;
char ch = getchar();
for(;!isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = x * + ch - ;
return x *= f;
} const int N = ; int n, C;
int h[N + ];
ll s[][N + ], dp[N + ]; ll solve(int x, int y, int mx) {
ll a = y - x - ;
ll b = - * (s[][y-] - s[][x]) - (x != ) * C - (y != n+) * C;
ll c = s[][y-] - s[][x] + 1ll * (x != ) * h[x] * C + 1ll * (y != n+) * h[y] * C; ll t;
t = (ll) ((- b / / a) + 0.5); chkmax<ll>(t, mx);
if(x != ) chkmin(t, (ll) h[x]);
if(y <= n) chkmin(t, (ll) h[y]); return a * t * t + b * t + c;
} int main() { read(n), read(C);
for(int i = ; i <= n; ++i) {
read(h[i]);
s[][i] = s[][i-] + h[i];
s[][i] = s[][i-] + 1ll * h[i] * h[i];
} static int stk[N + ], top; h[] = h[n + ] = ( << );
stk[top ++] = ; for(int i = ; i <= n+; ++i) {
dp[i] = dp[i-] + ((i == || i == n+) ? : 1ll * C * std::abs(h[i] - h[i-]));
while(top > && h[stk[top-]] <= h[i]) {
if(top > )
chkmin(dp[i], dp[stk[top-]] + solve(stk[top-], i, h[stk[top-]]));
-- top;
}
stk[top ++] = i;
}
printf("%lld\n", dp[n + ]); return ;
}

Problem 8 dp的更多相关文章

  1. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  2. [LightOJ1004]Monkey Banana Problem(dp)

    题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1004 题意:数塔的变形,上面一个下面一个,看清楚 ...

  3. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  4. (LightOJ 1004) Monkey Banana Problem 简单dp

    You are in the world of mathematics to solve the great "Monkey Banana Problem". It states ...

  5. 【UVA 1380】 A Scheduling Problem (树形DP)

    A Scheduling Problem   Description There is a set of jobs, say x1, x2,..., xn <tex2html_verbatim_ ...

  6. BZOJ 2302: [HAOI2011]Problem c( dp )

    dp(i, j)表示从i~N中为j个人选定的方案数, 状态转移就考虑选多少人为i编号, 然后从i+1的方案数算过来就可以了. 时间复杂度O(TN^2) ------------------------ ...

  7. BZOJ 2318: Spoj4060 game with probability Problem( 概率dp )

    概率dp... http://blog.csdn.net/Vmurder/article/details/46467899 ( from : [辗转山河弋流歌 by 空灰冰魂] ) 这个讲得很好 , ...

  8. hdu 5106 Bits Problem(数位dp)

    题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...

  9. hiho1259 A Math Problem (数位dp)

    题目链接:http://hihocoder.com/problemset/problem/1259 题目大意:g(t)=(f(i)%k=t)的f(i)的个数 求所有的(0-k-1)的g(i)的异或总值 ...

  10. BZOJ 2302: [HAOI2011]Problem c [DP 组合计数]

    2302: [HAOI2011]Problem c Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 648  Solved: 355[Submit][S ...

随机推荐

  1. [LOJ #2833]「JOISC 2018 Day 1」帐篷

    题目大意:有一个$n\times m$的网格图,若一个人的同一行或同一列有人,他就必须面向那个人,若都无人,就可以任意一个方向.若一个人无法确定方向,则方案不合法,问不同的方案数.$n,m\leqsl ...

  2. 7、注解@Mapper、@MapperScan

    7.注解@Mapper.@MapperScan 2018年09月20日 11:12:41 飞奔的加瓦 阅读数 3284    版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载. https ...

  3. 你有自信写while(true)吗?

    每次写while(true)的时候会不会心虚? 特别逻辑稍微复杂一点

  4. VS 引用dll版本冲突问题

    1.删除项目中的对应引用: 2.如果是有用到NetGet引用的删除项目中的packages里面的对应包文件: 3.如果是在NetGet中引用的注释项目中packages.config对应的插件名: 4 ...

  5. 从ghost映像.gho文件快速创建vmware虚拟机

    从ghost映像.gho文件快速创建vmware虚拟机 https://www.cnblogs.com/blog2018/p/8857146.html ghost文件.gho和vmware文件都是磁盘 ...

  6. Golang-使用md5对字符串进行加密

    方式一: func md5Test1(str string) string { m5 := md5.New() _,err := m5.Write([]byte(str)) if err != nil ...

  7. node+mysql+vue+express项目搭建

    第一步:项目搭建之前首先需要安装node环境和MySQL数据库. 在已经完成上述的条件下开始进行以下操作: npm install @vue/cli -g   (-g 代表全局安装) 初始化项目  v ...

  8. HelloWorld! C++纠错版

    例题:1 #include<iostream> int main() { cout << "HelloWorel!" ; ; } #include < ...

  9. Fatal error: Uncaught Error: Call to a member function bind_param() on boolean

    1.2019年10月22日 PHP写mysqli 预编译查询的时候报错. Fatal error: Uncaught Error: Call to a member function bind_par ...

  10. 微信小程序分享小程序码的生成,多参数以及参数的获取

    如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ 如果解决不了,可以在文末进群交流. 官方文档地址:https://developers.weixin.qq.com/minipro ...