Description

有\(N\)个左下定点为原点的矩阵, 每个矩阵\((x_i,~y_i)\)都有一个数\(a_i\)表示其花费。

没有一个矩阵包含另一个矩阵。

现要你选出若干个矩阵, 使得矩阵组成的图形的大小减去总花费得到的数最大

Solution

先按照\(x_i\) 递增排序, 由于矩阵互不包含, 所以\(y_i\)递减。 就可以列出\(DP\)方程:

\[f_i~=~(x_i~-~x_j)~ \times~ y_i~-~a_i~+~f_j~~~~~~~~(j<i)
\]

可以拆成斜率优化方程:

\[f_j~=~y_i~\times~x_j~+~a_i~-~x_i~\times~y_i~+~f_i
\]

上斜率优化板子就AC了, 复杂度\(O(N)\)

Code

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<vector>
  6. #define up(a, b) (a = a > b ? a : b)
  7. #define down(a, b) (a = a > b ? b : a)
  8. #define cmax(a, b) (a > b ? a : b)
  9. #define cmin(a, b) (a > b ? b : a)
  10. #define Abs(a) ((a) > 0 ? (a) : -(a))
  11. #define rd read()
  12. #define db double
  13. #define LL long long
  14. using namespace std;
  15. typedef pair<int, int> P;
  16. inline char nc(){
  17. static char buf[1<<14],*p1=buf,*p2=buf;
  18. return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<14,stdin),p1==p2)?EOF:*p1++;
  19. }
  20. inline LL read(){
  21. char c=nc();LL x=0,f=1;
  22. while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}
  23. while(c>='0'&&c<='9'){x=x*10+c-'0',c=nc();}
  24. return x*f;
  25. }
  26. const int N = 1e6 + 1e5;
  27. int n, q[N];
  28. LL ans = 0, f[N];
  29. struct node {
  30. int x, y;
  31. LL w;
  32. bool operator < (const node &b) const {
  33. return x < b.x;
  34. }
  35. }a[N];
  36. double calk(int A, int B) {
  37. double ax = a[A].x, bx = a[B].x;
  38. double ay = f[A], by = f[B];
  39. return (ay - by) / (ax - bx);
  40. }
  41. int main()
  42. {
  43. n = rd;
  44. for (int i = 1; i <= n; ++i) {
  45. a[i].x = rd;
  46. a[i].y = rd;
  47. a[i].w = rd;
  48. }
  49. sort(a + 1, a + 1 + n);
  50. int l = 1, r = 1;
  51. for (int i = 1; i <= n; ++i) {
  52. while (l < r && calk(q[l], q[l + 1]) >= a[i].y)
  53. l++;
  54. int tmp = q[l];
  55. f[i] = (1LL * a[i].x - a[tmp].x) * a[i].y - a[i].w + f[tmp];
  56. up(ans, f[i]);
  57. while (l < r && calk(q[r], q[r - 1]) <= calk(q[r], i))
  58. r--;
  59. q[++r] = i;
  60. }
  61. printf("%lld\n", ans);
  62. }

Codeforces 1083E The Fair Nut and Rectangles的更多相关文章

  1. CodeForces 1083 E The Fair Nut and Rectangles 斜率优化DP

    The Fair Nut and Rectangles 题意:有n个矩形,然后你可以选择k个矩形,选择一个矩形需要支付代价 ai, 问 总面积- 总支付代价 最大能是多少, 保证没有矩形套矩形. 题解 ...

  2. CF1083E The Fair Nut and Rectangles

    CF1083E The Fair Nut and Rectangles 给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有 ...

  3. CodeForces 1084D The Fair Nut and the Best Path

    The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...

  4. Codeforces 1083B The Fair Nut and Strings

    Description 给定两个由 \('a'\), \('b'\) 组成的字符串 \(a\), \(b\),以及两个整数 \(n\) 和 \(k\) \(n\) 表示字符串 \(a\),\(b\) ...

  5. Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings

    E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的 ...

  6. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path

    D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的 ...

  7. Codeforces Round #526 (Div. 2) C. The Fair Nut and String

    C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...

  8. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

  9. A. The Fair Nut and Elevator (Codeforces Round #526 (Div. 2))

    A. The Fair Nut and Elevator 好笨啊QAQ. 暴力枚举的题,连分类都不用. 从电梯初始位置到第一层.人到第一层.间隔的层数,往返路程. #include <bits/ ...

随机推荐

  1. intellij idea 汉化

    intellij idea是IDEA的全称,现在最新版本的是intellij idea 16.这是一款公认的比较好的用于编程的软件.但是每次软件的发布都是全英文的,这让英文不好的人很是头疼.现在我告诉 ...

  2. C++_数字时钟软件实现设计

    利用C++学习内容,通过windows自带函数实现一个简易的时钟 #include<iostream> #include<windows.h> //延时与清屏头文件 using ...

  3. JAVA Freemarker + Word 模板 生成 Word 文档 (普通的变量替换,数据的循环,表格数据的循环,以及图片的东替换)

    1,最近有个需求,动态生成 Word 文当并供前端下载,网上找了一下,发现基本都是用 word 生成 xml 然后用模板替换变量的方式 1.1,这种方式虽然可行,但是生成的 xml 是在是太乱了,整理 ...

  4. MyBatis 配制文件层次表

  5. pyhon的yileld的一点笔记

    yield感觉很神秘,感觉也不好理解,学习pyhon最后终归是要学习这个东西,研究了一段时间,把自己的笔记写下来 说简单点就是遇到yield就停止往下执行代码,也包括不执行yield这条语句,然后返回 ...

  6. mysql导入太慢解决方法

    半调子数据科学家又要折腾数据,拿到数据一看,3.6G的zip文件,解压看看,卧槽12个G的sql文件.好吧,又要折腾sql数据了.第一件事,肯定是搭一个数据库,导入数据咯. 折腾过sql导入的亲们都知 ...

  7. IDEA中使用中jetty启动java项目(非springboot)

    1.安装maven helper插件,略 2.项目pom.xml文件中添加jetty插件配置 <build> <plugins> <plugin> <grou ...

  8. SpringMVC对ServletAPI的支持和JSON格式的转换

    package com.hongcong.controller; import java.io.UnsupportedEncodingException; import java.net.URLDec ...

  9. Java8内置的函数式编程接口应用场景和方式

    首先,我们先定义一个函数式编程接口 @FunctionalInterface public interface BooleanFunctionalInterface<T> { boolea ...

  10. .Net23种设计模式

    C#常见的设计模式 一.概要: 模式分为三种,设计模式.体系结构模式与惯用法.其中惯用法是一种语言紧密相关的模式,例如,定界加锁模式其实是一种惯用法. 在C#项目开发过程中,很多情况下您已经使用了某些 ...