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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define up(a, b) (a = a > b ? a : b)
#define down(a, b) (a = a > b ? b : a)
#define cmax(a, b) (a > b ? a : b)
#define cmin(a, b) (a > b ? b : a)
#define Abs(a) ((a) > 0 ? (a) : -(a))
#define rd read()
#define db double
#define LL long long
using namespace std;
typedef pair<int, int> P; inline char nc(){
static char buf[1<<14],*p1=buf,*p2=buf;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<14,stdin),p1==p2)?EOF:*p1++;
}
inline LL read(){
char c=nc();LL x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}
while(c>='0'&&c<='9'){x=x*10+c-'0',c=nc();}
return x*f;
} const int N = 1e6 + 1e5; int n, q[N];
LL ans = 0, f[N]; struct node {
int x, y;
LL w;
bool operator < (const node &b) const {
return x < b.x;
}
}a[N]; double calk(int A, int B) {
double ax = a[A].x, bx = a[B].x;
double ay = f[A], by = f[B];
return (ay - by) / (ax - bx);
} int main()
{
n = rd;
for (int i = 1; i <= n; ++i) {
a[i].x = rd;
a[i].y = rd;
a[i].w = rd;
}
sort(a + 1, a + 1 + n);
int l = 1, r = 1;
for (int i = 1; i <= n; ++i) {
while (l < r && calk(q[l], q[l + 1]) >= a[i].y)
l++;
int tmp = q[l];
f[i] = (1LL * a[i].x - a[tmp].x) * a[i].y - a[i].w + f[tmp];
up(ans, f[i]);
while (l < r && calk(q[r], q[r - 1]) <= calk(q[r], i))
r--;
q[++r] = i;
}
printf("%lld\n", ans);
}

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. tomcat启动问题 严重: End event threw exception

    错误信息: 严重: End event threw exception java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebX ...

  2. Android开发 解决EditText与NestedScrollView 滚动冲突问题

    问题简述 在将EditText套进NestedScrollView的情况下,EditText输入了多行内容后,无法触摸滚动到第一行 问题原因 在将EditText套进NestedScrollView的 ...

  3. springboot学习随笔(二):简单的HelloWorld

    接上章搭建好springboot环境后,开始开发入门级HelloWorld 一.构建简单的springboot项目 1.新建项目,选择Spring/Spring Starter Project 2.N ...

  4. 嵌入式linux——汇编、C语言基础(一)

    一.汇编语言基础 (断断续续的记录自己的笔记...2018-10-11) 1. mov指令 基本用法: mov r0, #0 mov指令是赋值指令,用法如上,把立即数0放入到寄存器r0中. 2. bl ...

  5. sql server 新语法 收藏

    1.行转列 PIVOT函数,行转列,列转换UNPIVOT select * from ShoppingCart as C PIVOT(count(TotalPrice) FOR [Week] IN([ ...

  6. Jeecg-Boot 1.0版本发布,企业级快速开发平台

     Jeecg-Boot 一款基于代码生成器的J2EE快速开发框架! 采用前后端分离技术: SpringBoot,Mybatis-plus,Shiro,JWT,Vue & Ant Design. ...

  7. AQS原理以及AQS同步组件总结

    1 AQS简单介绍 2 AQS原理

  8. tcp那个孤独的小包到底怎么回事?

    内核3.10,接<tcp的发送端一个小包就能打破对端的delay_ack么?> 我们继续来分析这个没满mss的小包, 可以看到,由于受到syn ack这端是发包端,所以该发送链路协商的ms ...

  9. Redis系列十:缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级

    一.缓存雪崩 缓存雪崩我们可以简单的理解为:由于原有缓存失效,新缓存未到期间(例如:我们设置缓存时采用了相同的过期时间,在同一时刻出现大面积的缓存过期),所有原本应该访问缓存的请求都去查询数据库了,而 ...

  10. Mongodb到mysql数据库的数据迁移(Java,Windows)

    运行环境为windows 测试过260万的数据表,迁移大概要10分钟左右,当然肯定和网络,字段大小什么的有关系. 遇到的坑和注意点都用紫色标记了(对,就是我大乃团的高冷紫--Nogizaka 46) ...