Solution -「LOCAL」逃生
\(\mathcal{Description}\)
有 \(n\) 个人掉进了深度为 \(h\) 的坑里,第 \(i\) 个人的肩高为 \(a_i\),臂长为 \(b_i\)。设当前坑里人的集合为 \(S\),第 \(i\) 人能逃生,当且仅当 \(\sum_{j\in S}a_j+b_i\ge h\)。求最多逃生人数。
\(n\le2\times10^5\)。
\(\mathcal{Solution}\)
考虑在最优情况下,相邻两个逃生的人,设其肩高臂长分别为 \((a,b),(p,q)\),未逃生者肩高之和 \(s\),则现在有:
s+b\ge h\\
s-a+q\ge h
\end{cases}
\]
尝试交换两人顺序:
s+q\ge h\\
s-p+b\ge h
\end{cases}
\]
不难发现 \(b-p\le q-a\) 时,前式可推出后时,可结合题意理解为“前者逃生,考虑为后者手变短,那么后者手越长越优”。依此排序。
此后,顺序扫一遍,若当前人能逃生直接逃生。但为保证最优,我们需要最大化坑里人的 \(\sum a_i\)。这时考虑一个反悔操作——用逃生的一个人来替换当前这个人。显然由于贪心的排序,替换一定成立,只要逃生的人的肩高大于当前人,替换就会优化答案,所以直接抓肩高最大的人回坑里就好。
复杂度 \(\mathcal O(n\log n)\)。
\(\mathcal{Code}\)
#include <queue>
#include <cstdio>
#include <algorithm>
typedef long long LL;
inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
}
const int MAXN = 2e5;
int n, H;
LL sum;
std::priority_queue<int> heap;
struct Person {
int a, b;
inline void read () { a = rint (), b = rint (); }
inline bool operator < ( const Person t ) const { return b - t.a < t.b - a; }
} per[MAXN + 5];
int main () {
freopen ( "escape.in", "r", stdin );
freopen ( "escape.out", "w", stdout );
n = rint ();
for ( int i = 1; i <= n; ++ i ) per[i].read (), sum += per[i].a;
H = rint ();
int ans = 0;
std::sort ( per + 1, per + n + 1 );
for ( int i = 1; i <= n; ++ i ) {
heap.push ( per[i].a );
if ( sum + per[i].b >= H ) ++ ans;
else sum += heap.top (), heap.pop ();
sum -= per[i].a;
}
printf ( "%d\n", ans );
return 0;
}
\(\mathcal{Details}\)
也是够勇的,不拍就过了这道智慧贪心题 owo!
Solution -「LOCAL」逃生的更多相关文章
- Solution -「LOCAL」二进制的世界
\(\mathcal{Description}\) OurOJ. 给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...
- Solution -「LOCAL」大括号树
\(\mathcal{Description}\) OurTeam & OurOJ. 给定一棵 \(n\) 个顶点的树,每个顶点标有字符 ( 或 ).将从 \(u\) 到 \(v\) ...
- Solution -「LOCAL」过河
\(\mathcal{Description}\) 一段坐标轴 \([0,L]\),从 \(0\) 出发,每次可以 \(+a\) 或 \(-b\),但不能越出 \([0,L]\).求可达的整点数. ...
- Solution -「LOCAL」Drainage System
\(\mathcal{Description}\) 合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个.每次可以挑 \(x\in[L,R]\) ...
- Solution -「LOCAL」Burning Flowers
灼之花好评,条条生日快乐(假装现在 8.15)! \(\mathcal{Description}\) 给定一棵以 \(1\) 为根的树,第 \(i\) 个结点有颜色 \(c_i\) 和光亮值 ...
- Solution -「LOCAL」画画图
\(\mathcal{Description}\) OurTeam. 给定一棵 \(n\) 个点的树形随机的带边权树,求所有含奇数条边的路径中位数之和.树形生成方式为随机取不连通两点连边直到全 ...
- Solution -「LOCAL」ZB 平衡树
\(\mathcal{Description}\) OurOJ. 维护一列二元组 \((a,b)\),给定初始 \(n\) 个元素,接下来 \(m\) 次操作: 在某个位置插入一个二元组: 翻 ...
- Solution -「LOCAL」舟游
\(\mathcal{Description}\) \(n\) 中卡牌,每种三张.对于一次 \(m\) 连抽,前 \(m-1\) 次抽到第 \(i\) 种的概率是 \(p_i\),第 \(m\) ...
- Solution -「LOCAL」充电
\(\mathcal{Description}\) 给定 \(n,m,p\),求序列 \(\{a_n\}\) 的数量,满足 \((\forall i\in[1,n])(a_i\in[1,m])\l ...
随机推荐
- c# - 常量定义与赋值
1.前言 c#与Java很相似,但是不一样,又与js(JavaScript)相似,但是也不一样,所以我认为c#是Java和 js的孩子. 2.常量定义 字符串: const string = &quo ...
- 我的2021年度总结-回忆录|附旅行Vlog
今天是农历腊月初十,还有20天就是2022年了.这一年,些许遗憾,些许期盼.时间久了,很多事已经慢慢模糊了,只记得,这最后几个月的闲碎小事. 不止多久,很久没有码字了.有些事,记不清,忆不得.时至今年 ...
- Solon 开发,三、构建一个Bean的三种方式
Solon 开发 一.注入或手动获取配置 二.注入或手动获取Bean 三.构建一个Bean的三种方式 四.Bean 扫描的三种方式 五.切面与环绕拦截 六.提取Bean的函数进行定制开发 七.自定义注 ...
- 《剑指offer》面试题40. 最小的k个数
问题描述 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 示例 1: 输入:arr = [3,2,1], k ...
- 【刷题-LeetCode】151 Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- java-异常-原理异常对象的抛出throw
1 class Demo { 2 public static int method(int[] arr,int index) { 3 4 // System.out.println(arr[index ...
- python使用pip安装库超时报错解决办法
原因:pip源超时了,安装不上 pip install matplotlib -i http://pypi.douban.com/simple --trusted-host pypi.douban.c ...
- Avoiding the Backup of Online Redo Logs
Although it may seem that you should back up online redo logs along with the datafiles and control f ...
- python 定义函数关键字def 简单介绍
一 在类中定义的def # python中def 是用来干什么的? # 可以定义函数,就是定义一个功能. class People(): def __init__(self): print(&quo ...
- AWS 模拟题知识点总结!
一 题库的地址 https://www.lleicloud.com/index.php/aws-certified-saa-c01-practice-questions-c6-01/ 二 总结的知识点 ...