CF1083E The Fair Nut and Rectangles
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的更多相关文章
- CodeForces 1083 E The Fair Nut and Rectangles 斜率优化DP
The Fair Nut and Rectangles 题意:有n个矩形,然后你可以选择k个矩形,选择一个矩形需要支付代价 ai, 问 总面积- 总支付代价 最大能是多少, 保证没有矩形套矩形. 题解 ...
- Codeforces 1083E The Fair Nut and Rectangles
Description 有\(N\)个左下定点为原点的矩阵, 每个矩阵\((x_i,~y_i)\)都有一个数\(a_i\)表示其花费. 没有一个矩阵包含另一个矩阵. 现要你选出若干个矩阵, 使得矩阵组 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- 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的 ...
- 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 题意: 给出一棵树,走不重复的 ...
- 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的子序列( ...
- 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 ...
随机推荐
- JS之console.log详解以及兄弟姐们邻居方法扩展
console.log() 基本用法 console.log,前端常用它来调试分析代码,你可以在任何的js代码中调用console.log(),然后你就可以在浏览器控制台看到你刚才打印的常量,变量,数 ...
- springmvc 获取请求头信息
@PostMapping("/test/post") public void post(@RequestBody String params, @RequestHeader(req ...
- 求二叉树第n层节点数
在知乎看到今日头条的一个面试题“求二叉树第n层节点数”:https://zhuanlan.zhihu.com/p/25671699,想到了这样一个解法,欢迎大家交流 我的解法采用递归的思想,从0层开始 ...
- Genymotion安卓模拟器和VirtualBox虚拟机安装、配置、测试
Genymotion安卓模拟器和VirtualBox虚拟机安装.配置.测试(win7_64bit) 目录 1.概述 2.本文用到的工具 3.VirtualBox虚拟机安装 4.Genymotion安卓 ...
- Nginx 配置下载附件让浏览器提示用户是否保存
Nginx配置下载附件让浏览器提示用户是否保存 by:授客 QQ:1033553122 测试环境 nginx-1.10.0 问题描述: 前端页面,IE11浏览器下请求下载附件模板,针对xls ...
- 图说OOP基础(一)
本文用图形化的形式描述OOP的相关知识.对OOP进行系统化的梳理,以便掌握,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: OOP的相关知识 OOP知识总图 [Object-Orientat ...
- ubuntu12.0.4开启root用户登陆
1.命令:sudo passwd root 为root分配密码,按提示进行设置就好. 2.打开终端,输入以下命令: sudo -s 进入root账户下: cd /etc/lightdm g ...
- codeforces 735C Tennis Championship(贪心+递推)
Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C ——每天在线,欢迎留言谈论. 题目大意: 给你一个 n ...
- MySQL 安装及卸载详细教程
本文采用最新版MySQL8版本作为安装教程演示,本人亲试过程,准确无误.可供读者参考. 下载 官网下载 --> 社区免费服务版下载. 下载Windows安装程序MySQL Installer M ...
- ctypes库调用dll的个人见解
最近着手开发一个小东西涉及到了API接口的知识点, 第一次使用到了ctypes库,在网上找了一大圈,基本都是讲add.dll之后就没了. 就像下面这个: from ctypes import * dl ...