CF1083E The Fair Nut and Rectangles

给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有权值 \(a_i\) 。你的任务是选出若干个矩形,使得选出的矩形的面积并减去矩形的权值之和尽可能大。输出最大值。

\(1\leq n\leq10^6,\ 1\leq x_i,\ y_i\leq10^9,\ 0\leq a_i\leq x_i\cdot y_i\)

斜率优化


因为所有矩阵互不包含,所以先按 \(x_i\) 升序, \(y_i\) 降序排个序

令 \(f_i\) 为,截止第 \(i\) 个矩阵,选取任意个矩阵所能得到的最大值

则: $$f_i=\displaystyle\max_{j<i}{f_j+S_i-S_i\cap S_j-a_i}$$

因为 \(x_i\) 升序, \(y_i\) 降序,所以 $$f_i=\displaystyle\max_{j<i}{f_j+x_iy_i+x_jy_i-a_i}$$

接着套路地化式子,得到 $$f_j=y_ix_j+f_i+a_i-x_iy_i$$

令 \(Y=f_j,\ K=y_i,\ X=x_j\) ,因为 \(y_i\) 单调递减,所以直接维护即可

时间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std; #define nc getchar()
typedef long long ll;
typedef long double db;
const int maxn = 1e6 + 10;
ll f[maxn];
int n, q[maxn]; struct node {
ll x, y, w;
bool operator < (const node& o) const {
return y > o.y;
}
} a[maxn]; inline ll read() {
ll x = 0;
char c = nc;
while (c < 48) c = nc;
while (c > 47) x = (x << 3) + (x << 1) + (c ^ 48), c = nc;
return x;
} db slope(int x, int y) {
return a[x].x == a[y].x ? 1e18 : db(f[x] - f[y]) / db(a[x].x - a[y].x);
} int main() {
n = read();
for (int i = 1; i <= n; i++) {
a[i].x = read();
a[i].y = read();
a[i].w = read();
}
ll ans = 0;
int l = 1, r = 1;
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
while (l < r && slope(q[l], q[l + 1]) > a[i].y) l++;
f[i] = f[q[l]] + (a[i].x - a[q[l]].x) * a[i].y - a[i].w;
while (l < r && slope(q[r - 1], q[r]) < slope(q[r], i)) r--;
q[++r] = i, ans = max(ans, f[i]);
}
printf("%I64d", ans);
return 0;
}

orz \(\color{black}{S}\color{red}{ooke}\)

CF1083E The Fair Nut and Rectangles的更多相关文章

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

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

  2. Codeforces 1083E The Fair Nut and Rectangles

    Description 有\(N\)个左下定点为原点的矩阵, 每个矩阵\((x_i,~y_i)\)都有一个数\(a_i\)表示其花费. 没有一个矩阵包含另一个矩阵. 现要你选出若干个矩阵, 使得矩阵组 ...

  3. CF 1083 B. The Fair Nut and Strings

    B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析:  建出trie树,给定的两个字符串就 ...

  4. CF 1083 A. The Fair Nut and the Best Path

    A. The Fair Nut and the Best Path https://codeforces.com/contest/1083/problem/A 题意: 在一棵树内找一条路径,使得从起点 ...

  5. CF1083A The Fair Nut and the Best Path

    CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...

  6. 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的 ...

  7. 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 题意: 给出一棵树,走不重复的 ...

  8. 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的子序列( ...

  9. C. The Fair Nut and String 递推分段形dp

    C. The Fair Nut and String 递推分段形dp 题意 给出一个字符串选择一个序列\({p_1,p_2...p_k}\)使得 对于任意一个\(p_i\) , \(s[p_i]==a ...

随机推荐

  1. CSS效果:CSS3实现模拟select以及其中的三角形

    如图实现这样的效果: html代码如下: <div class="select-bg"> <select class="select-green&quo ...

  2. 2018-12-16 VS Code英汉词典进化效果演示: 翻译文件所有命名

    续VS Code英汉词典插件v0.0.7-尝试词性搭配, 下一个功能打算实现文件的批量命名翻译: 批量代码汉化工具 · Issue #86 · program-in-chinese/overview ...

  3. Sublime Text 2 2.0.2 序列号

    ----- BEGIN LICENSE -----Andrew WeberSingle User LicenseEA7E-855605813A03DD 5E4AD9E6 6C0EEB94 BC9979 ...

  4. 花十分钟,让你变成AI产品经理

    花十分钟,让你变成AI产品经理 https://www.jianshu.com/p/eba6a1ca98a4 先说一下你阅读本文可以得到什么.你能得到AI的理论知识框架:你能学习到如何成为一个AI产品 ...

  5. mysql数据库的备份和恢复

    Mysql数据库的备份和恢复 1.备份单个数据库 mysql数据库自带了一个很好用的备份命令,就是mysqldump,它的基本使用如下: 语法:mysqldump –u <用户名> -p ...

  6. Loadrunner 脚本开发-利用loadrunner开发Windows Sockets协议脚本

    脚本开发-利用loadrunner开发Windows Sockets协议脚本 by:授客 QQ:1033553122 欢迎加入软件性能测试交流QQ群:7156436 实践举例 Socket服务端简单实 ...

  7. LeetCode题解之Clone Graph

    1.题目描述 2.问题分析 要遍历图,然后标记没有被复制的节点. 3.代码 class Solution { private: unordered_map<Node*, Node*> m; ...

  8. Django之--POST方法处理表单请求

    上一篇:Django之--MVC的Model 演示了如何使用GET方法处理表单请求,本文讲述直接在当前页面返回结果,并使用更常用的POST方法处理. 一.首先我们修改下page.html <!D ...

  9. 数据挖掘---Numpy的学习

    什么是Numpy NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵(任意维度的数据处理),比Python自身的嵌套列表(nested list structure ...

  10. 【2018.08.19 C与C++基础】编程语言类型系统简介(草稿)

    还是先占坑,等理顺了思路再写,学过的东西总是无法系统化,感觉什么都知道一点,但一深入却是一脸懵逼. 这真的是个问题,看似很努力,却无法成为一个master. 参考链接: 1. 编程语言的类型系统为何如 ...