codeforces319C
2 seconds
256 megabytes
standard input
standard output
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money.
The manager of logging factory wants them to go to the jungle and cut n trees with heights a1, a2, ..., an. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height ai in the beginning), then the cost of charging the chain saw would be bi. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, ai < aj and bi > bj and also bn = 0 and a1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost.
They want you to help them! Will you?
The first line of input contains an integer n (1 ≤ n ≤ 105). The second line of input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line of input contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 109).
It's guaranteed that a1 = 1, bn = 0, a1 < a2 < ... < an and b1 > b2 > ... > bn.
The only line of output must contain the minimum cost of cutting all the trees completely.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
5
1 2 3 4 5
5 4 3 2 0
25
6
1 2 3 10 20 30
6 5 4 3 2 0
138
题意:给你一把电锯,现在有编号为1---n的n棵树,第i棵树有两个属性:树的高度ai和到当前树充一格电的花费bi。你每次选一棵树进行砍伐,只有你将当前的树砍完后才能进行充电和砍其他树,
并且你只能到已经被砍伐的树充电。你每砍一个单位长度的树要消耗一格电,你的电锯初始有一格电,现在问你最少要多少电费可以将所有树砍完。
题目保证树的高度是递增的,充电花费是递减的。第一棵树的高度是1,最后一棵树处充电花费是0。
思路:简单的斜率优化dp
由题意易知我们开始肯定是先砍第一棵树,我们只要找到将第n棵树砍掉的最小花费就可以了,其他树我们可以无消耗砍完
易推出状态转移方程:f[i]=min(f[j]+a[i]*b[j]);(j<i)
转换为一般的直线方程
f[j]=-a[i]*b[j]+f[i]
y = kx + b
我们只要维护一个斜率递减的凸包就可以了
代码:
#include<cstdio>
#include<algorithm>
#define ll long long
#define N 100010
using namespace std;
ll a[N],b[N];
int que[N];
ll f[N];
double X(int i){
return b[i];
}
double Y(int i){
return f[i];
}
double rate(int i,int j){
return (Y(i)-Y(j))/(X(i)-X(j));
}
int main(){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]);
for(int i=;i<=n;i++)
scanf("%I64d",&b[i]);
int head,tail;
head=tail=;
f[]=;
que[]=;
for(int i=;i<=n;i++){
while(tail>head&&rate(que[head],que[head+])>-a[i])head++;
int j=que[head];
f[i]=f[j]+a[i]*b[j];
while(tail>head&&rate(que[tail],que[tail-])<rate(que[tail],i))tail--;que[++tail]=i;
}
printf("%I64d\n",f[n]);
}
codeforces319C的更多相关文章
随机推荐
- 一、ribbon如何集成在openfeign中使用
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 ribbon是springcloud封装的一个基于http客户端负载均衡的组件.spri ...
- mysql日期模糊查找的方法
Mysql模糊查询有以下三种方法: 1.Convert转成日期时间型,在用Like查询.select * from table1 where convert(date,DATETIME) like ' ...
- Vue注意事项
在使用Vue中的函数或自己定义的函数或指令的时候,Vue说明如下 在一些自己定义或系统定义的驼峰命名规则的时候,你需要到元素区域引用的使用中间的大写要改成小写在谭家 一条横杠如: 你在var=new ...
- 关于MUI页面之间传值以及刷新的问题
一.页面刷新问题 1.父页面A跳转到子页面B,B页面修改数据后再跳回A页面,刷新A页面数据 (1).父页面A代码 window.addEventListener("pageflowrefre ...
- SQLSEVER在存储过程或触发器中模糊查询拼接
declare @name nvarchar(50); declare @name_pin nvarchar(50); set @name_pin = '%'+@name +'%' 模糊查询: sel ...
- Ubuntu:一个部署好的tomcat应用(war包)怎么用Nginx实现动静分离?
今天想把之前的一个demo用Nginx把资源分离开来,在网上看了一天,整整弄了一天,硬是没弄出来. 要么全是同样的内容的,要么就是环境跟我这里不一样的.再加上对Nginx没接触过,给我都整哭了差点. ...
- 用ffserver实现rtsp服务器的实验笔记
参考:https://blog.csdn.net/hoyjam1/article/details/51281679 建议配置文件:/etc/config/ffserver.conf Port 1053 ...
- 《Python编程:从入门到实践》第三章 列表简介 习题答案
#3.1 names=['lpr','tjl','gnl','by','dqy']; print(names[0]); print(names[1]); print(names[2]); print( ...
- java kafka
https://blog.csdn.net/panchang199266/article/details/82113453 安装部署 scala语言开发 + java
- Java中list在循环中删除元素的坑
JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常说的foreach循环).iterator遍历. 1.for循环遍历list for(int i=0;i<list.siz ...