$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. Python3基础语法(20190617)

    字符串 字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"xyz"等等.请注意,''或""本身只是一种表示方式,不是字符串的一部分,因此 ...

  2. Redis Streams与Spark的完美结合

    来源:Redislabs 作者:Roshan Kumar 翻译:Kevin  (公众号:中间件小哥) 最近,我有幸在 Spark +AI 峰会上发表了题目为“Redis + Structured St ...

  3. The Day Two 找到一个具有最大和的连续子数组,返回其最大和

    """ 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5, ...

  4. Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂

    Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...

  5. Linux实现MYSQl数据库的定时备份

    今天给大家分享一下如何在Linux下实现MYSQl数据库的定时备份. 前提需要保证你的Linux服务器已经安装了MYSQl数据库服务. 1.创建shell脚本 vim backupdb.sh 创建脚本 ...

  6. Microsoft Visual Studio常用快捷键

    快速补全关键字 1)tab; 删除整行代码 1)Ctrl + L; 回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键“Ctrl + -”; 2)前进到下一个光标位置:“Ct ...

  7. Invalid escape sequence at line 1 column 29 path $[0].name

    编译报错:Invalid escape sequence at line 1 column 29 path $[0].name 解决:grade.properties 文件下 org.gradle.j ...

  8. 从汇编语言写到c语言

    好了,言归正传,裸机程序没有操作系统的支持,想要用c语言,就只能先使用汇编语言手动配置c语言需要的环境,听起来很高大上,其实需要做的很简单:指定堆栈指针的值就好. 那么完成这个简单的事情之前,先要明白 ...

  9. js常用的数组去重方法

    给出以下数组 var arr1 = new Array('1','2','3','3','5','4','5','6','5','5','88'); 实现对数组的去重 1. 双重for循环去重 Arr ...

  10. WPF登录功能,对于密码框的操作,其实WPF有个PasswordBox专门的密码框控件,完全可以选择自己要显示的密码符号。

    在链接数据库后,点击登录时需要判断用户名和密码框是否为空,而PasswordBox不像textbox那样判断 textbox判断文本框为空 if (this.UserName.Text.Trim()= ...