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 = 0and 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.

Sample test(s)
input
  1. 5
  2. 1 2 3 4 5
  3. 5 4 3 2 0
output
  1. 25
input
  1. 6
  2. 1 2 3 10 20 30
  3. 6 5 4 3 2 0
output
  1. 138

这题就是斜率DP(555...早知道直接做第3题)

方程我就不写了

long long 溢出我居然De了一上午Bug才发现,我……

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<functional>
  7. #include<cmath>
  8. #include<cctype>
  9. using namespace std;
  10. #define For(i,n) for(int i=1;i<=n;i++)
  11. #define Rep(i,n) for(int i=0;i<n;i++)
  12. #define Fork(i,k,n) for(int i=k;i<=n;i++)
  13. #define ForD(i,n) for(int i=n;i;i--)
  14. #define Forp(x) for(int p=pre[x];p;p=next[p])
  15. #define RepD(i,n) for(int i=n;i>=0;i--)
  16. #define MEM(a) memset(a,0,sizeof(a))
  17. #define MEMI(a) memset(a,127,sizeof(a))
  18. #define MEMi(a) memset(a,128,sizeof(a))
  19. #define MAXN (100000+10)
  20. #define size (tail-head+1)
  21. int n;
  22. long long a[MAXN],b[MAXN],f[MAXN]={0};
  23. struct node
  24. {
  25. int i;
  26. long long x,y;
  27. node(){}
  28. node(int _i):i(_i),x(b[i]),y(f[i]){}
  29. friend long double k(node a,node b){return (long double)(a.y-b.y)/(long double)(a.x-b.x); }
  30. }q[MAXN];
  31. int head=1,tail=1;
  32. struct V
  33. {
  34. long long x,y;
  35. V(node a,node b):x(b.x-a.x),y(b.y-a.y){}
  36. friend long long operator*(V a,V b){return ((double)a.x/a.y-(double)b.x/b.y)>0?1:-1; }
  37. };
  38. int main()
  39. {
  40. // freopen("CF319C.in","r",stdin);
  41. // freopen("CF319C.out","w",stdout);
  42.  
  43. cin>>n;
  44. For(i,n) cin>>a[i];
  45. For(i,n) cin>>b[i];
  46. f[1]=b[1];
  47. q[1]=node(1);
  48. Fork(i,2,n)
  49. {
  50. while (size>=2&&k(q[head],q[head+1])>1-a[i] ) head++;
  51. int j=q[head].i;
  52. f[i]=f[j]+(long long)(a[i]-1)*(long long)b[j]+b[i];
  53. while (size>=2&&(V(q[tail-1],q[tail])*V(q[tail],node(i))>0)) tail--; //维护上凸壳
  54. q[++tail]=node(i);
  55. }
  56. //For(i,n) cout<<b[i]<<' ';cout<<endl;
  57. //For(i,n) cout<<f[i]<<' ';cout<<endl;
  58. cout<<f[n]<<endl;
  59.  
  60. return 0;
  61. }

CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)的更多相关文章

  1. CF 319C - Kalila and Dimna in the Logging Industry 斜率优化DP

    题目:伐木工人用电锯伐木,一共需要砍n棵树,每棵树的高度为a[i],每次砍伐只能砍1单位高度,之后需要对电锯进行充电,费用为当前砍掉的树中最大id的b[id]值.a[1] = 1 , b[n] = 0 ...

  2. 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 ] ) ...

  3. CF R 635 div1 C Kaavi and Magic Spell 区间dp

    LINK:Kaavi and Magic Spell 一打CF才知道自己原来这么菜 这题完全没想到. 可以发现 如果dp f[i][j]表示前i个字符匹配T的前j个字符的方案数 此时转移变得异常麻烦 ...

  4. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

  5. DP的优化总结

    一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...

  6. codeforces319C

    C. Kalila and Dimna in the Logging Industry time limit per test 2 seconds memory limit per test 256 ...

  7. Codeforces Round #189 (Div. 1 + Div. 2)

    A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...

  8. 关于学习汇编的一些规则的理解(div mul cf of)

    ->1.div(除法)  被除数/除数 ->一共有三种格式的除法,分别是8位,16位,32位,这里的位数表示的是除数的位数 ->实现div其实也很简单就是除数为被除数的一半就ok - ...

  9. CF和OF的区别

    进位标志CF和溢出标志OF的区别: 有符号数和无符号数只是认为的进行区分,计算机从来不区分有符号数和无符号数.对于运算的数来说,只要符合进位的情况,CF就置1.只要符合溢出的情况,OF就置1.但是后续 ...

随机推荐

  1. 新到的Mac配置Java开发环境

    今天Mac到手,需要配置一些用到的开发环境,在这里做一些纪录. 1. 下载Eclipse,地址:http://www.eclipse.org/downloads/,因为个人需求,所以下载的是Java ...

  2. 复制virtualenv环境到其他服务器环境配置的方法

    要在n多服务器端部署python的应用,虽然python本身是跨平台的,当时好多第三方的扩展却不一定都能做到各个版本兼容,即便是都是linux,在redhat系列和ubuntu系列之间来回导也是个很让 ...

  3. __call重载方法

    <?php class Person { function du() { echo "这是一个存在的方法"; } //该方法有两个参数,第一个参数 $function_nam ...

  4. 监测scroll

    $(window).scroll(function() { var scrollValue = $(this).scrollTop(); var h=200+scrollValue; $('.yui3 ...

  5. mybatis通用DAO

    扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. 回复:茶爸爸了解他这个人!! 花了几天的时间研究了一下mybatis的源代码,觉得这 ...

  6. poj 1503 Integer Inquiry (高精度运算)

    题目链接:http://poj.org/problem?id=1503 思路分析: 基本的高精度问题,使用字符数组存储然后处理即可. 代码如下: #include <iostream> # ...

  7. poj 1084 Brainman(归并排序)

    题目链接:http://poj.org/problem?id=1804 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可. 根据分治法思想,序列分为两个大小相等的两部分,分别求子序列的逆 ...

  8. Apache Common StringUtils工具类

    1.List转换成字符串并加入分隔符 public String listToString(List list, char separator) { return org.apache.commons ...

  9. 在主函数中提示用户输入用户名和密码。另写一方法来判断用户输入是否正确。该方法分别返回一个bool类型的登录结果和和一个string类型的登录信息。如登录成功,返回true及“登录成功”,若登录失败则返回false及“用户名错误”或“密码错误”(使用out参数)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. linux 修改IP, DNS 命令

    linux 修改IP, DNS 命令 http://www.cnblogs.com/fighter/archive/2010/03/04/1678007.html 修改DNS [root@localh ...