CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)
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 = 0and 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
这题就是斜率DP(555...早知道直接做第3题)
方程我就不写了
long long 溢出我居然De了一上午Bug才发现,我……
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMI(a) memset(a,127,sizeof(a))
#define MEMi(a) memset(a,128,sizeof(a))
#define MAXN (100000+10)
#define size (tail-head+1)
int n;
long long a[MAXN],b[MAXN],f[MAXN]={0};
struct node
{
int i;
long long x,y;
node(){}
node(int _i):i(_i),x(b[i]),y(f[i]){}
friend long double k(node a,node b){return (long double)(a.y-b.y)/(long double)(a.x-b.x); }
}q[MAXN];
int head=1,tail=1;
struct V
{
long long x,y;
V(node a,node b):x(b.x-a.x),y(b.y-a.y){}
friend long long operator*(V a,V b){return ((double)a.x/a.y-(double)b.x/b.y)>0?1:-1; }
};
int main()
{
// freopen("CF319C.in","r",stdin);
// freopen("CF319C.out","w",stdout); cin>>n;
For(i,n) cin>>a[i];
For(i,n) cin>>b[i];
f[1]=b[1];
q[1]=node(1);
Fork(i,2,n)
{
while (size>=2&&k(q[head],q[head+1])>1-a[i] ) head++;
int j=q[head].i;
f[i]=f[j]+(long long)(a[i]-1)*(long long)b[j]+b[i];
while (size>=2&&(V(q[tail-1],q[tail])*V(q[tail],node(i))>0)) tail--; //维护上凸壳
q[++tail]=node(i);
}
//For(i,n) cout<<b[i]<<' ';cout<<endl;
//For(i,n) cout<<f[i]<<' ';cout<<endl;
cout<<f[n]<<endl; return 0;
}
CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)的更多相关文章
- CF 319C - Kalila and Dimna in the Logging Industry 斜率优化DP
题目:伐木工人用电锯伐木,一共需要砍n棵树,每棵树的高度为a[i],每次砍伐只能砍1单位高度,之后需要对电锯进行充电,费用为当前砍掉的树中最大id的b[id]值.a[1] = 1 , b[n] = 0 ...
- Codeforces Round #189 (Div. 1) C - Kalila and Dimna in the Logging Industry 斜率优化dp
C - Kalila and Dimna in the Logging Industry 很容易能得到状态转移方程 dp[ i ] = min( dp[ j ] + b[ j ] * a[ i ] ) ...
- CF R 635 div1 C Kaavi and Magic Spell 区间dp
LINK:Kaavi and Magic Spell 一打CF才知道自己原来这么菜 这题完全没想到. 可以发现 如果dp f[i][j]表示前i个字符匹配T的前j个字符的方案数 此时转移变得异常麻烦 ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- DP的优化总结
一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...
- codeforces319C
C. Kalila and Dimna in the Logging Industry time limit per test 2 seconds memory limit per test 256 ...
- Codeforces Round #189 (Div. 1 + Div. 2)
A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...
- 关于学习汇编的一些规则的理解(div mul cf of)
->1.div(除法) 被除数/除数 ->一共有三种格式的除法,分别是8位,16位,32位,这里的位数表示的是除数的位数 ->实现div其实也很简单就是除数为被除数的一半就ok - ...
- CF和OF的区别
进位标志CF和溢出标志OF的区别: 有符号数和无符号数只是认为的进行区分,计算机从来不区分有符号数和无符号数.对于运算的数来说,只要符合进位的情况,CF就置1.只要符合溢出的情况,OF就置1.但是后续 ...
随机推荐
- UIView 设置阴影(属性说明)
以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowCo ...
- LNMP安装包sh脚本
Xshell 5 (Build 0719) Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved. Type `he ...
- [Python]小笔记-queue
queue的作用: 队列最大的作用就是先进先出(First in First Out).队列对于解决最短路的时候特别好用. python 2.7: 要使用队列,那么要加载头文件Queue,也就是imp ...
- poj 3252
http://poj.org/problem?id=3252//自己搞了很长时间...现在刚刚有点明白.. 1 #include <iostream> using namespace st ...
- Qt学习 之 数据库(支持10种数据库)
Qt 提供了 QtSql 模块来提供平台独立的基于 SQL 的数据库操作.这里我们所说的“平台独立”,既包括操作系统平台,有包括各个数据库平台.另外,我们强调了“基于 SQL”,因为 NoSQL 数据 ...
- cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(五)-地图卷动
马里奥在平移的过程中,涉及到地图的卷动问题. 在这个游戏里,地图比窗口大,窗口只是显示了地图的一部分,因此马里奥在移动的时候,移动到一定位置之后要卷动地图,否则马里奥移动到窗口右边之后......那结 ...
- 基于Bresenham算法画圆
bresenham算法画圆思想与上篇 bresenham算法画线段 思想是一致的 画圆x^2+y^2=R^2 将他分为8个部分,如上图 1. 只要画出1中1/8圆的圆周,剩下的就可以通过对称关系画出这 ...
- STL之vector详解
一.vector容器的自增长 首先,我们知道vector容器是由数组做出来的:它具备了数组的优缺点. 数组的优点: 操作数据,读取速度很快,因为有下标: 数组的缺点: 分配之后不能在改变大小: #in ...
- SQL语句简单记录
SQL SERVER 新增列与默认值 alter table 表名 add 列明 bit default 0 not null 删除列(容易删除失败) alter table 表名 drop colu ...
- iOS开发之理解iOS中的MVC设计模式
模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程 ...