题意

题目链接

Sol

枚举第二个球放的位置,用前缀和推一波之后发现可以斜率优化

  1. // luogu-judger-enable-o2
  2. #include<bits/stdc++.h>
  3. #define Pair pair<int, int>
  4. #define MP(x, y) make_pair(x, y)
  5. #define fi first
  6. #define se second
  7. #define int long long
  8. #define LL long long
  9. #define Fin(x) {freopen(#x".in","r",stdin);}
  10. #define Fout(x) {freopen(#x".out","w",stdout);}
  11. using namespace std;
  12. const int MAXN = 1e6 + 10, mod = 1e9 + 7;
  13. LL INF = 2e18 + 10;
  14. const double eps = 1e-9;
  15. template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
  16. template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
  17. template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
  18. template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
  19. template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
  20. template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
  21. template <typename A> inline void debug(A a){cout << a << '\n';}
  22. template <typename A> inline LL sqr(A x){return 1ll * x * x;}
  23. inline int read() {
  24. char c = getchar(); int x = 0, f = 1;
  25. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  26. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  27. return x * f;
  28. }
  29. int N, w[MAXN], d[MAXN], s[MAXN], sw[MAXN], g[MAXN], f[MAXN], q[MAXN];
  30. int calc(int l, int r) {
  31. if(l > r) return 0;
  32. return s[r] - s[l - 1] - d[l - 1] * (sw[r] - sw[l - 1]);
  33. }
  34. double Y(int x) {
  35. return g[x];
  36. }
  37. double X(int x) {
  38. return d[x];
  39. }
  40. double slope(int a, int b) {
  41. return double(Y(b) - Y(a)) / (X(b) - X(a));
  42. }
  43. signed main() {
  44. N = read();
  45. for(int i = 1; i <= N; i++) w[i] = read(), d[i] = read();
  46. reverse(w + 1, w + N + 1); reverse(d + 1, d + N + 1);
  47. for(int i = 1; i <= N; i++) d[i] += d[i - 1], s[i] = s[i - 1] + w[i] * d[i], sw[i] = w[i] + sw[i - 1];
  48. LL ans = INF;
  49. for(int i = 1; i <= N; i++) g[i] = calc(1, i - 1)- s[i] + d[i] * sw[i];
  50. q[1] = 0;
  51. for(int i = 1, h = 1, t = 1; i <= N; i++) {
  52. f[i] = INF;
  53. while(h < t && slope(q[h], q[h + 1]) < sw[i - 1]) h++;
  54. f[i] = g[q[h]] - d[q[h]] * sw[i - 1];
  55. while(h < t && slope(q[t - 1], q[t]) > slope(q[t], i)) t--;
  56. q[++t] = i;
  57. //for(int j = i - 1; j >= 1; j--) chmin(f[i], g[j] - d[j] * sw[i - 1]);
  58. f[i] += s[i - 1];
  59. }
  60. for(int i = 1; i <= N; i++)
  61. f[i] += calc(i + 1, N), chmin(ans, f[i]);
  62. cout << ans;
  63. return 0;
  64. }
  65. /*
  66. */

洛谷P4360 [CEOI2004]锯木厂选址(dp 斜率优化)的更多相关文章

  1. 2018.08.28 洛谷P4360 [CEOI2004]锯木厂选址(斜率优化dp)

    传送门 一道斜率优化dp入门题. 是这样的没错... 我们用dis[i]表示i到第三个锯木厂的距离,sum[i]表示前i棵树的总重量,w[i]为第i棵树的重量,于是发现如果令第一个锯木厂地址为i,第二 ...

  2. 洛谷P4360 [CEOI2004]锯木厂选址(斜率优化)

    传送门 我可能根本就没有学过斜率优化…… 我们设$dis[i]$表示第$i$棵树到山脚的距离,$sum[i]$表示$w$的前缀和,$tot$表示所有树运到山脚所需要的花费,$dp[i]$表示将第二个锯 ...

  3. 洛谷4360[CEOI2004]锯木厂选址 (斜率优化+dp)

    qwq 我感觉这都已经不算是斜率优化\(dp\)了,感觉更像是qwq一个\(下凸壳优化\)转移递推式子. qwq 首先我们先定义几个数组 \(sw[i]\)表示\(w[i]\)的前缀和 \(val[i ...

  4. 【洛谷 P4360】 [CEOI2004]锯木厂选址(斜率优化)

    题目链接 一开始我的\(dp\)方程列错了,其实也不能说列错了,毕竟我交上去还是把暴力的分都拿到了,只是和题解的不一样,然后搞半天没搞出来去看题解,又看不懂,对不上,原来状态设置不一样自闭了. \(f ...

  5. P4360 [CEOI2004]锯木厂选址

    P4360 [CEOI2004]锯木厂选址 这™连dp都不是 \(f_i\)表示第二个锯木厂设在\(i\)的最小代价 枚举1号锯木厂 \(f_i=min_{0<=j<i}(\sum_{i= ...

  6. 【BZOJ2684】【CEOI2004】锯木厂选址(斜率优化,动态规划)

    [BZOJ2684][CEOI2004]锯木厂选址(斜率优化,动态规划) 题面 万恶的BZOJ因为权限题的原因而做不了... 我要良心的提供题面 Description 从山顶上到山底下沿着一条直线种 ...

  7. luogu4360 锯木厂选址 (斜率优化dp)

    设: sw[i]为1..i的w之和 sd[i]为1到i的距离 cost[i]为把第一个锯木厂建在i带来的花费 all[i,j]为把i..j所有木头运到j所需要的花费 所以$all[i,j]=cost[ ...

  8. [BSOJ2684]锯木厂选址(斜率优化)

    Description 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂.木材只能按照一个方向运输:朝山下运.山脚下有一个锯木厂 ...

  9. luogu P4360 [CEOI2004]锯木厂选址

    斜率优化dp板子题[迫真] 这里从下往上标记\(1-n\)号点 记\(a_i\)表示前缀\(i\)里面树木的总重量,\(l_i\)表示\(i\)到最下面的距离,\(s_i\)表示\(1\)到\(i-1 ...

随机推荐

  1. JPA总结——实体关系映射(一对多@OneToMany)

    JPA总结——实体关系映射(一对多@OneToMany) 注意:本文出自“阿飞”的博客,如果要转载本文章,请与作者联系! 并注明来源: http://blog.sina.com.cn/s/blog_4 ...

  2. Concurrent包工具类使用

    一.读写锁 传统的同步锁就是独占式锁,当线程使用资源时候保持独占,无论读写.当人们发现请求队列(假设)中相邻请求为读-读的时候,阻塞是一种浪费资源的操作.比如公告板,所有路过的人(请求)都是读操作,并 ...

  3. Android Ble4.0开发

    最近在做一个蓝牙的项目,是Blu4.0,Android系统支持是4.3版本以上(为此特地卖了个手机,). 当然也参考了不少大牛的的微博,说的都不错,再次特地感谢 http://blog.csdn.ne ...

  4. 监督学习——决策树理论与实践(上):分类决策树

    1. 介绍         决策树是一种依托决策而建立起来的一种树.在机器学习中,决策树是一种预测模型,代表的是一种对象属性与对象值之间的一种映射关系,每一个节点代表某个对象/分类,树中的每一个分叉路 ...

  5. 巧用border特性实现聊天气泡效果

    利用border特性,实现三角形,很简单,我们直接看效果: html: <div class="bubble-container ">你好么 <div class ...

  6. 补间动画Tweened Animations

    本例子简单讲一下怎么用补间动画 1.在xml中定义好动画的资源文件,如下(注意把不同的效果放在一起可以一起用,同时起效果) <?xml version="1.0" encod ...

  7. 关于注解Annotation第二篇

    写一个注解使用类,如下: public class Test { @Code(author = "mazhi",date="20170611") private ...

  8. MySQL的Sleep进程占用大量连接解决方法

    第一部分为产生大量sleep进程的原理及对应解决方法第二部分为设置wait_timeout值,有效减少sleep进程 ========================================= ...

  9. 用css实现在横线中间插入文字

    在一些登录或者注册界面中常常可看到,一个大框框上面的横线中有插入文字,大概示意图如下: 这种现象其实是相对位置作用的结果,可以通过相对定位,是某元素恰好出现在div的边框上. position: re ...

  10. springboot-6-整合jdbc

    如果有整合jpa了, 那么在dao中直接, 不需要引入依赖 @Resource private JdbcTempalte jdbcTempalte; 如果没有的话, 就先在pom.xml中加入依赖 & ...