$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. Codeforces Round #570 Div. 3

    A:暴力从小到大枚举判断. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 10 ...

  2. go 学习笔记(4) import

    package main import ( f "fmt" ) const NAME string = "imooc" var a string = " ...

  3. Java中 单例模式(singleton)

    Java Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 使用Singleton的好处还在于可以节省内存,因为它限制了实例的个数,有利于Java垃圾回收( ...

  4. SWD下载k60

    转:JTAG各类接口针脚定义,含义及SWD接线方式 IAR设置如下

  5. 【洛谷 P2483】 【模板】k短路([SDOI2010]魔法猪学院)(A*)

    题目链接 优先队列bfs第一次出队就是最短路,那么显然第k次出队就是k短路 ?????????????????????????????? 书上写的 但是直接优先队列bfs会T,所以用A*优化就行,估价 ...

  6. 浅析JavaScript异步

    一直以来都知道JavaScript是一门单线程语言,在笔试过程中不断的遇到一些输出结果的问题,考量的是对异步编程掌握情况.一般被问到异步的时候脑子里第一反应就是Ajax,setTimseout...这 ...

  7. Python定义点击右上角关闭按钮事件

    Python定义点击右上角关闭按钮事件(Python defines the event of clicking the close button in the upper right corner) ...

  8. Topshelf+Quartz实现windows任务

    Topshelf使用示例, HostFactory.Run(x => { x.Service<QuartzStartup>(s => { s.ConstructUsing(na ...

  9. python代码工具小结

    目录: 1.with读.写文件 (1)with读文件 (2)with写文件 2.requests爬虫 (1)get请求 (2)post请求 1.with读.写文件 (1)with读文件 (2)with ...

  10. linux-秘钥生成

    服务器sshd配置 #vim /etc/ssh/sshd_conf PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys # ...