C. Kalila and Dimna in the Logging Industry
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 < jai < 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?

Input

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.

Output

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.

Examples
input

Copy
5
1 2 3 4 5
5 4 3 2 0
output

Copy
25
input

Copy
6
1 2 3 10 20 30
6 5 4 3 2 0
output

Copy
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的更多相关文章

随机推荐

  1. 【转载】网站配置Https证书系列(一):腾讯云申请免费的SSL证书的流程步骤(即https安全连接使用的证书)

    很多网站为了安全性考虑都会上https安全连接,此时就需要考虑使用SSL证书,其实在腾讯云这边提供有免费的SSL证书申请,登录腾讯云管理控制台后,进入SSL证书管理页面,里面有个申请免费证书.腾讯云申 ...

  2. elementui限制开始日期和结束日期

    项目需求:开始日期和结束日期 禁用当前日期之前的日期.同时结束日期 禁用开始日期之前的日期 <div class='startTime'> 开始时间:<el-date-picker ...

  3. IO模型之NIO代码及其实践详解

    一.简介 NIO我们一般认为是New I/O(也是官方的叫法),因为它是相对于老的I/O类库新增的( JDK 1.4中的java.nio.*包中引入新的Java I/O库).但现在都称之为Non-bl ...

  4. 分布式session的几种实现方式

    在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理.如果不做任何处理的话,用户将出现频繁登录的现象,比如集群中存在A.B两台服务器,用户在第一次访问网站时,Nginx通过 ...

  5. VirtualBox、VMware在桥接模式下无法获取ip地址问题

    声明: 参考 https://blog.csdn.net/lcdcxy/article/details/49362171 https://jingyan.baidu.com/article/948f5 ...

  6. golang Methods on structs

    原文:http://golangtutorials.blogspot.com/2011/06/methods-on-structs.html snmp 下载,有空学习一下! https://sourc ...

  7. 大数据之路week06--day07(Linux中的mysql的离线安装)

    这里我提供 服务端和客户端的两个jar包的百度云,也是我使用的 链接:https://pan.baidu.com/s/11a3LT-ENZ8n9IF19-VjmWA 提取码:bdls 离线安装Mysq ...

  8. 安装pytest

    1.安装pytest 2.执行一个用例 进入测试用例目录下,运行以test开头的一个用例. 执行成功. 备注:1.其实测试函数或方法只要以test开头就可以被运行的2.测试文件的名字,其实可以是任意的 ...

  9. thymeleaf引入公共css、js

    有时候很多css文件是公共的,我们必须要在每个html文件中引入它们,其实我们可以利用Thymeleaf的模板布局,把这些css文件抽出来,同时如果有某个html文件专属的css文件,还可在引入模板的 ...

  10. Windows 创建Raid

    Windows 常见raid有0.1和5,以下操作在虚拟机下模拟,学会这招在自己电脑做个raid也未尝不可啊~ 一.RAID 0 创建: 添加两块硬盘,联机并初始化(2T以下选MBR,以上选GPT) ...