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的更多相关文章
随机推荐
- jdk1.8 接口default方法
jdk1.8 版本新增了一些特性,与之前版本差异相对.若不清楚地话,在使用过程中会产生很大的疑问. 本次介绍的是interface接口中方法的特殊性. 在以前jdk版本在接口中是只允许定义方法方法名, ...
- 【转载】使用Response.WriteFile输出文件以及图片
Response对象是Asp.Net应用程序中非常重要的一个内置对象,其作用为负责将服务器执行好的信息输出给客户端,可以使用Response.WriteFile方法来像客户端输出文件或者图片,输出图片 ...
- 在vue项目中使用live2d
成品如图: 那么几步简单说明怎么用吧: 第一,先去github上下载相应的静态资源: https://github.com/xiazeyu/live2d-widget-models 第二,将packg ...
- Python:GeoJson格式的多边形裁剪Tiff影像并计算栅格数值
JSON是通过键值对表示数据对象的一种格式,其全称为JavaScript Object Notation,它采用完全独立于编程语言的文本格式来存储和表示数据,轻量级.简洁清晰的层次结构.容易解析等特点 ...
- impala 表迁移方式 partquet数据文件移动方法
1.原表查询:select count(*) from edm.ucard_wxd0123 where stat_dt = '2024-01-09' and id_no = '110101199003 ...
- 修改mysql/MariaDB数据库的端口号+远程
1.修改端口 2.远程+开放端口 (1)设置远程账号:xxx和密码yyyyyyygrant all privileges on *.* to 'xxx'@'%' identified by 'yyyy ...
- sql的时间格式
sql中的时间格式转换主要有:date_format函数,str_to_date函数 1. 首先选择一个数据库 use db_name; 2. 显示当前时区的时间: SELECT NOW(); 3. ...
- SCOI2018 树
树 时间限制 3000ms 内存限制 64MB [题目描述] 在大小为 N 的树上,点从 1 到 N 标号,第 i 个点有权值 Ai,现在需要支持两种操作: 第一种操作格式为"1 U&quo ...
- Git报错:Please tell me who you are.
Git在提交的时候报错 Please tell me who you are. 报错 Please tell me who you are. 具体如下: 原因:明确报错.请告诉我你是谁.意思是你在提交 ...
- python中的lambda()函数
语句:print map(lambda x:x ** 2,[1,2,3,4,5]) 其中lambda()函数在Python文档,文档中解释如下: lambda An anonymous inline ...