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. [待优化笔记]原生JS实现验证框架 checkFun

    ;(function(){ /** 验证框架 checkFun * 使用方法: * <input class="required" type="text" ...

  2. 如何用ABP框架快速完成项目(8) - 用ABP一个人快速完成项目(4) - 能自动化就不要手动 - 使用自动化测试(BDD/TDD)

    做为一个程序员, 深深知道计算机自动化的速度是比人手动的速度快的, 所以”快速”完成项目的一个重要武器就是: 能自动化就不要手动.   BDD/TDD有很多优势, 其中之一就是自动化, 我们这节文章先 ...

  3. 小程序实践(三):九宫格实现及item跳转

    效果图: 实现效果图红色线包含部分的九宫格效果,并附带item点击时间. --------------------------------------------------------------- ...

  4. (其他)sublime text3的emmt插件的简便用法

  5. 修改minifest使桌面软件支持高dpi

    在VisualStudio中可以很方便的设置manifest以支持高dpi的用户界面.当然也可以手工修改manifest文件来添加对高dpi的支持. QQ在高dpi方面做的尤其差,对高dpi的支持迟迟 ...

  6. python3接收、解析邮件

    邮件接收 python3可以使用poplib.POP3进行邮件接收,具体如下: import poplib from email.parser import Parser def get_email( ...

  7. mssql sqlserver 禁止删除数据表中指定行数据(转自:http://www.maomao365.com/?p=5323)

    转自:http://www.maomao365.com/?p=5323 摘要:下文主要讲述,如何禁止删除数据表中指定行数据 最近收到用户一个需求,禁止所有人删除”表A”中,ID 为1.2.3.4.5的 ...

  8. javaweb分页查询实现

    Javaweb分页技术实现 分页技术就是通过SQL语句(如下)来获取数据,具体实现看下面代码 //分页查询语句 select * from 表名 where limit page , count; 和 ...

  9. Oracle EBS R12 GL_IMPORT_REFERENCES 映射

    非原创. 转自出处: http://alloracleapps.com/oracle_apps/gl_import_references-columns-mapping-11i-vs-r12/

  10. SQL Prompt 快捷键

    推荐一个小插件,SQL Prompt,配合Microsoft SQL Server Management Studio,使用起来非常方便,同时再加上以下几个快捷键: (1)ctrl+5或F5,运行代码 ...