Problem 8 dp
$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的更多相关文章
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- [LightOJ1004]Monkey Banana Problem(dp)
题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1004 题意:数塔的变形,上面一个下面一个,看清楚 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- (LightOJ 1004) Monkey Banana Problem 简单dp
You are in the world of mathematics to solve the great "Monkey Banana Problem". It states ...
- 【UVA 1380】 A Scheduling Problem (树形DP)
A Scheduling Problem Description There is a set of jobs, say x1, x2,..., xn <tex2html_verbatim_ ...
- BZOJ 2302: [HAOI2011]Problem c( dp )
dp(i, j)表示从i~N中为j个人选定的方案数, 状态转移就考虑选多少人为i编号, 然后从i+1的方案数算过来就可以了. 时间复杂度O(TN^2) ------------------------ ...
- BZOJ 2318: Spoj4060 game with probability Problem( 概率dp )
概率dp... http://blog.csdn.net/Vmurder/article/details/46467899 ( from : [辗转山河弋流歌 by 空灰冰魂] ) 这个讲得很好 , ...
- hdu 5106 Bits Problem(数位dp)
题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...
- hiho1259 A Math Problem (数位dp)
题目链接:http://hihocoder.com/problemset/problem/1259 题目大意:g(t)=(f(i)%k=t)的f(i)的个数 求所有的(0-k-1)的g(i)的异或总值 ...
- BZOJ 2302: [HAOI2011]Problem c [DP 组合计数]
2302: [HAOI2011]Problem c Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 648 Solved: 355[Submit][S ...
随机推荐
- UI测试用例设计,场景测试法
百度一番,没有发现详细的UI测试用例设计方法,只能自己整理一下,学习.改进. 那么正题来了,我们慢慢缕下思路: 1.整理要测实体中的,处理逻辑.触发规则.动作. 2.将场景测试抽象出来 3.到这个时候 ...
- 环境配置--升级Python 3.6爬坑
升级到3.6之后,发现ctrl alt t呼不出命令台,找了半天发现update manager也打不开,而且没有错误报告.....查阅了一番资料看到有人有类似的问题(https://askubunt ...
- WPF 程序如何移动焦点到其他控件
原文:WPF 程序如何移动焦点到其他控件 WPF 中可以使用 UIElement.Focus() 将焦点设置到某个特定的控件,也可以使用 TraversalRequest 仅仅移动焦点.本文介绍如何在 ...
- 用JS实现一个斗地主发牌器
//调用随机数,在我上一篇博文讲过这一个函数. function roundNum(min = 0, max = 0) { if (!isNaN(min) && !isNaN(max) ...
- springboot+security整合(1)
说明 springboot 版本 2.0.3源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+se ...
- python+selenium爬取关键字搜索google图片
# -*- coding: utf-8 -*- import json import os import time from multiprocessing import Pool import mu ...
- (在客户端)https抓包解密
(一)原理分析 https的数据包是用对称秘钥(https协议协商出来的随机数)加密后的密文. 对称秘钥在传输线路上是密文的(被非对称加密过),但是在client.server端是明文的(因为要用于加 ...
- MySQL数据库的事物隔离级别
一. 查看数据库的事物隔离级别 mysql> show variables like '%isolation'; +-----------------------+--------------- ...
- DRF 有无外键操作实例
models.py from django.db import models # Create your models here. class Category(models.Model): &quo ...
- 更新yum源并重建缓存
原文连接 1)下载wget yum install -y wget 2)备份默认的yum mv /etc/yum.repos.d /etc/yum.repos.d.backup 3)设置新的yum目录 ...