当前位置:网站首页>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;
}
边栏推荐
- 数据库sql语言实战
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- Detailed explanation of the whole process of coding's pressure measurement operation
- 【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
- 对接微信支付(二)统一下单API
- Kingbasees SQL language reference manual of Jincang database (9. Common DDL clauses)
- Three graduates, three years of experience in embedded software
- 知识沉淀一:架构师是做什么?解决了什么问题
- The idea YML file code does not prompt the solution
- 逆序打印链表
猜你喜欢

Mba-day28 concept of number - exercise questions

Is it really hopeless to choose electronic engineering and be discouraged?

对接微信支付(二)统一下单API

金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)

unity 像素画导入模糊问题
![[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism](/img/19/32206eb6490c2a5a7a8e746b5003c1.png)
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism

Redis持久化-AOF

"Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree

You'd better not take this kind of project!

Ros2 knowledge: DDS basic knowledge
随机推荐
柠檬班自动化学习毕竟
leetcode-aboutString
二叉树的性质 ~
idea yml 文件代码不提示解决方案
Redis发布订阅
flex布局
Establishment of log collection and analysis platform-1-environment preparation
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情
Should we test the Dao layer?
[paper notes] anti packet loss joint coding for network speech steganography
A company installs monitoring for each station: write code only to see employees?
Redis持久化-RDB
字节面试题——判断一棵树是否为平衡二叉树
某公司给每个工位装监控:只为看员工写代码?
1.12 basis of Web Development
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
软件测试面试题全网独家没有之一的资深测试工程师面试题集锦
Unity Profiler
K. Link with Bracket Sequence I dp