当前位置:网站首页>H. Take the Elevator 贪心
H. Take the Elevator 贪心
2022-07-26 05:48:00 【Strezia】
H
贪心
题意

贴的题解中的题意,这里其实写错了,楼高 k k k, 载客量限制为 m m m 。
思路
因为下降的过程中间不能转换方向,而上行的人肯定乘坐上行时的电梯,下行的人一定乘坐下行时的电梯(废话),所以不妨将人分为上行和下行两个方向来考虑。可以发现这两者是类似的,下面先考虑上行的人。
先考虑简单的情况:在每次上升的途中至多乘坐 m m m 人。而电梯的运行时间显然只和到达楼层最高的那个人有关,因此,在这种情况下我们每次贪心地选到达高度前 m m m 高的人乘坐电梯是最优解。
在此基础上,需要在每次上升的途中乘坐尽可能多的人。因此只要从上到下按终点高度尽可能地时时刻刻选 m m m 个人即可。
具体而言,我们可以记录每个上行的人的起始位置 s t st st 和终点位置 e d ed ed,利用前缀和维护当前电梯里有多少个人,如果超过 m m m 人则需要乘坐下一趟电梯,在这个过程中记录一共需要多少趟上行电梯以及它们需要到达的最高楼层。下行同理,具体见代码。
代码
int n, m, k;
struct Node {
int pos, d; //pos表示位置,d=1表示上电梯 d=-1表示下电梯
bool operator < (const Node &x) const {
if(pos != x.pos) return pos > x.pos;
return d < x.d;
}
};
vector<Node> up, down;
void solve() {
cin >> n >> m >> k;
for(int i = 1; i <= n; i++) {
int st, ed;
cin >> st >> ed;
if(st < ed) {
up.pb({
st, -1});
up.pb({
ed, 1});
}
else {
down.pb({
st, 1});
down.pb({
ed, -1});
}
}
sort(up.begin(), up.end());
sort(down.begin(), down.end());
int cnt = 0;
vector<int> up_max, down_max; //表示每次上升/下降到达的最大高度,size()表示次数
for(auto x : up) {
cnt += x.d; //维护当前电梯里的人数,如果超过则需要等下一次电梯
if(cnt > up_max.size() * m) up_max.pb(x.pos);
}
for(auto x : down) {
cnt += x.d;
if(cnt > down_max.size() * m) down_max.pb(x.pos);
}
int mx = max(up_max.size(), down_max.size());//总往返次数,为单程中的最大值。
int ans = 0;
for(int i = 0; i < mx; i++) {
cnt = 0;
if(up_max.size() > i) {
chmax(cnt, up_max[i]);
}
if(down_max.size() > i) {
chmax(cnt, down_max[i]);
}
ans = ans + (cnt-1) * 2;
}
cout << ans << endl;
}
边栏推荐
- 520 for what? DIY is a high-value RGB clock that girls want to watch
- Use of feign (Part 2)
- 如何查看Pod里容器名称
- How to understand "array name is essentially an address" from the perspective of memory parsing?
- It's enough for newcomers to learn how to do functional tests
- Should we test the Dao layer?
- Lemon class automatic learning after all
- 为什么LPDDR不能完全代替DDR?
- [paper notes] anti packet loss joint coding for network speech steganography
- Ros2 preliminary: basic communication with topic
猜你喜欢

选电子工程被劝退,真的没前景了?

Ros2 preliminary: basic communication with topic

平衡二叉树(AVL) ~

电机控制专栏文章汇总

Jdbc流式查询与游标查询

517. Super washing machine

某公司给每个工位装监控:只为看员工写代码?

Full binary tree / true binary tree / complete binary tree~
![[MySQL must know and know] time function number function string function condition judgment](/img/b2/aa15bf4cd78a3742704f6bd5ecb9c6.png)
[MySQL must know and know] time function number function string function condition judgment

软件测试面试题全网独家没有之一的资深测试工程师面试题集锦
随机推荐
STL common template library
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
[cloud native] record of feign custom configuration of microservices
柠檬班自动化学习毕竟
Binary sort tree (BST)~
Motor control column summary
一招教你轻松看懂波特图
520送什么?DIY一个高颜值RGB时钟,女生看了都想要
SSH Remote Management
三本毕业,三年嵌入式软件的心路历程
平衡二叉树(AVL) ~
vagrant下载速度慢的解决方法
Usage and common problems of SIP softphone registered with SIP account
How to name the project version number? Looks like cow b
虚拟偶像代言产品出问题谁负责? 且听律师分析
JDBC streaming query and cursor query
秋招-准备计划
Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
金仓数据库 KingbaseES SQL 语言参考手册 (11. SQL语句:ABORT 到 ALTER INDEX)
Mongodb common commands