当前位置:网站首页>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;
}
边栏推荐
- Redis主从复制
- C language explanation series - understanding of functions (4) declaration and definition of functions, simple exercises
- Knowledge points of Polymer Physics
- Jdbc流式查询与游标查询
- Select sort / insert sort / bubble sort
- 电机控制专栏文章汇总
- Motor control column summary
- 520送什么?DIY一个高颜值RGB时钟,女生看了都想要
- ES Cluster in Red status: what about write & delete operations?
- 520 for what? DIY is a high-value RGB clock that girls want to watch
猜你喜欢

柠檬班自动化学习毕竟

Application of canoe XML in test modules

How to understand "array name is essentially an address" from the perspective of memory parsing?

电机控制专栏文章汇总
![ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’](/img/15/25ff1e544565e18319ecca85e614a2.png)
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’

Mba-day29 arithmetic - preliminary understanding of absolute value

Why can't lpddr completely replace DDR?
![[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

Redis publish subscription

Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
随机推荐
高效,可靠,安全的串口通讯开源方案
Redis persistence AOF
金仓数据库 KingbaseES SQL 语言参考手册 (11. SQL语句:ABORT 到 ALTER INDEX)
[MySQL must know and know] time function number function string function condition judgment
高分子物理知识点
开发项目事半功倍,一款开源的stm32驱动库大集合
5-year-old Test Engineer - how to choose the next step?
某公司给每个工位装监控:只为看员工写代码?
Redis持久化-RDB
sdc中对cdc的处理方式
解决Vagrant报错b:48:in `join‘: incompatible character encodings: GBK and UTF-8 (Encoding::Compatib
Lamp architecture
为什么LPDDR不能完全代替DDR?
Application of canoe XML in test modules
Three implementation methods of thread and the usage of handler
Redis主从复制
Usage and common problems of SIP softphone registered with SIP account
FTP experiment and overview
How to understand "array name is essentially an address" from the perspective of memory parsing?
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’