当前位置:网站首页>H. Take the elevator greedy
H. Take the elevator greedy
2022-07-26 05:56:00 【Strezia】
H
greedy
The question

The meaning of the title in the posted Title Solution , There is actually a mistake here , High floor k k k, The passenger capacity is limited to m m m .
Ideas
Because the direction cannot be changed during the descent , And those who go up must take the elevator when they go up , People who go down must take the elevator when they go down ( crap ), Therefore, we might as well divide people into two directions: upward and downward . It can be found that the two are similar , Let's first consider the ascending people .
Consider the simple case first : Take at most on each ascent m m m people . And the running time of the elevator is obviously Only with the one who reaches the highest floor About that person , therefore , In this case, we greedily choose to reach the height every time m m m It is the optimal solution for tall people to take the elevator .
On this basis , You need to take as many people as possible on each ascent . So long as Press the height of the finish line from top to bottom, and select it as often as possible m m m personal that will do .
To be specific , We can record the starting position of each ascending person s t st st And end position e d ed ed, Use prefix and maintain how many people are in the current elevator , If exceeded m m m People need to take the next elevator , In this process, record the total number of upward elevators and the highest floor they need to reach . The same goes for the downlink , See code for details .
Code
int n, m, k;
struct Node {
int pos, d; //pos Indicate location ,d=1 It means getting on the elevator d=-1 Means getting off the elevator
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; // Means every rise / The maximum height reached by descent ,size() Number of times
for(auto x : up) {
cnt += x.d; // Maintain the current number of people in the elevator , If it exceeds, you need to wait for the next elevator
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());// Total round trips , Is the maximum value in one-way .
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;
}
边栏推荐
- Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
- Redis持久化-RDB
- Mysql45 talking about global lock, table lock and row lock
- Kingbasees SQL language reference manual of Jincang database (9. Common DDL clauses)
- Mba-day28 concept of number - exercise questions
- leetcode-Array
- Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
- 平衡二叉树(AVL) ~
- “子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
- Mysql45 speak in simple terms index
猜你喜欢

Ros2 knowledge: DDS basic knowledge

Using easyexcel to import tables to realize batch insertion of xlsx files ----- MySQL of Linux

Modifiers should be declared in the correct order

MBA-day29 算术-绝对值初步认识

The time complexity of two recursive entries in a recursive function

ETCD数据库源码分析——Cluster membership changes日志

vagrant下载速度慢的解决方法

Mysql45 talking about global lock, table lock and row lock

语法泛化三种可行方案介绍

Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
随机推荐
[STM32 series summary] blogger's way to quickly advance STM32 in actual combat (continuous update)
How to view the container name in pod
idea yml 文件代码不提示解决方案
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
[论文笔记] 面向网络语音隐写的抗分组丢失联合编码
金仓数据库 KingbaseES SQL 语言参考手册 (5. 操作符)
金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
[cloud native] record of feign custom configuration of microservices
招标信息获取
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
平衡二叉树(AVL) ~
Another open source artifact, worth collecting and learning!
Chapter 2 - getting started
Redis主从复制
leetcode-Array
柠檬班自动化学习毕竟
L. Link with Level Editor I dp
Chapter 1 - Construction of development environment
Blurring of unity pixel painting
CANoe-XML在Test Modules中的应用