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. MySQL5.7: datetime

    -- 当前日期时间 select now(); select now(3);-- 保留3位毫秒数 SELECT NOW(6); -- 保留6位毫秒数 -- 当前日期和时间 至秒 select curr ...

  2. 2018-05-14 代码考古-Python3官方教程字典例程

    知乎原链 Data Structures中的第一个例程: >>> tel = {'jack': 4098, 'sape': 4139} >>> tel['guido ...

  3. iOS ----------怎么修改xcode默认打开方式

    很简单就能解决:选中文件,右键,显示简介,打开方式,选择8.2.然后打钩.

  4. Future FutrueTask Callable类源码说明以及原理使用

    1.Future Callable FutureTask 源码说明 JDK内置的Future主要使用到了Callable接口和FutureTask类. Callable是类似于Runnable的接口, ...

  5. 「Android」GreenDao

    译文 版本:greenDAO 3.2.2 官网:http://greenrobot.org/greendao/ GitHub:https://github.com/greenrobot/greenDA ...

  6. 性能测试 查看Android APP 帧数FPS的方法

    (下述需要先安装eclipse,不然无法抓包) 1.保证手机与PC连接是正常的 2.打开手机“设置”→“开发者选项”(没有开发者选项就点击“关于手机”“版本号”连续点击就会出现开发者选项了).找到监控 ...

  7. python使用sax实现xml解析

    之前在使用xml解析的时候,在网上搜了很多教程,最终没有能按照网上的教程实现需求. 所以呢,只好自己去看源码,在sax的__init__.py下看到这么一段代码: 1 def parse(source ...

  8. Spark数据倾斜及解决方案

    一.场景 1.绝大多数task执行得都非常快,但个别task执行极慢.比如,总共有100个task,97个task都在1s之内执行完了,但是剩余的task却要一两分钟.这种情况很常见. 2.原本能够正 ...

  9. 如何在HTTP客户端与服务器端之间保持状态(转)

    HTTP协议与状态保持 HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端只需要简单的向服务器请求下载某些文件,无论是客户端还是服务器都没有必要纪录彼此过去的行为,每一次请求之间都 ...

  10. [20170611]关于数据块地址的计算.txt

    [20170611]关于数据块地址的计算.txt --//如果数据库出现一些问题,会在alert或者跟踪文件,或者屏幕出现一些错误提示.例如: ORA-00600: internal error co ...