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的更多相关文章
随机推荐
- linux环境下jdk安装
1,下载jdk版本 jdk-7u25-linux-x64.tar.gz 和windows jdk一致,jvm有区别: 2,拷贝到 /home目录下.通过tar -zxvf jdk-7u25-linu ...
- Python Scrapy 实战
Python Scrapy 什么是爬虫? 网络爬虫(英语:web crawler),也叫网络蜘蛛(spider),是一种用来自动浏览万维网的网络机器人.其目的一般为编纂网络索引. Python 爬虫 ...
- Linux 非互联网环境安装依赖包
1 介绍 有的生产环境是没有网络的,我们部署rpm包的时候会出现缺少很多rpm包的依赖问题,都去网上下载实在太麻烦,今天介绍一个办法可以解决这一问题. 2 解决方案 找一台可以联网的机器,在上边下载相 ...
- python之迭代器、生成器及列表推导式
一.迭代器 迭代器就是迭代的工具,迭代是一个重复的过程,每次重复都是一次迭代并且每次迭代的结果都是下次迭代的初始值. lst=[1,2,3,4,5] count=0 while count<le ...
- Hadoop读写mysql
需求 两张表,一张click表记录某广告某一天的点击量,另一张total_click表记录某广告的总点击量 建表 CREATE TABLE `click` ( `id` ) NOT NULL AUTO ...
- 《3+1团队》第七次作业:团队项目设计完善&编码
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...
- CORS通信
CORS 是一个 W3C 标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨域的服务器,发出XMLHttpRequest请 ...
- nginx 环境 thinkphp 隐藏index.php
tp官网已经写了 http://doc.thinkphp.cn/manual/hidden_index.html 不生效 重启nginx .问题依旧 kill掉nginx进程 再启动 贴段自己的配置 ...
- vue 仿新闻项目笔记
1.main.js: import filters from 'XXX' Object.keys(filters).forEach(key => Vue.filter(key, filters[ ...
- Selenium常用API的使用java语言之15-警告框处理
在 WebDriver中处理JavaScript所生成的alert.confirm以及prompt十分简单,具体做法是使用switch_to_alert()方法定位到alert/confirm/pro ...