当前位置:网站首页>2022牛客多校训练第二场 H题 Take the Elevator
2022牛客多校训练第二场 H题 Take the Elevator
2022-08-05 00:18:00 【Rain Sure】
题目链接
题目大意
大概意思就是,给了我们一部损坏的电梯,以及一些需要乘坐电梯的乘客的信息以及电梯的最大容量m。问我们电梯把所有乘客都运送到指令楼层所需要的最小时间。
题解
很明显的贪心题,但是不太好想,在这里给一个思路。
我们分别考虑上升的乘客和下降的乘客,因为电梯如果开始下降了,那么它就不能再上升了;所以我们让电梯每一躺尽可能走的高,那么每一趟所花费的时间就是上升的下降取一个最大的;
主要实现方法:
假设当前移动区间为 [ l , r ] [l, r] [l,r],假设 l < r l < r l<r,那么当前即为上升方向,我们在 l l l处-1,在 r r r处+1,也就是进电梯-1,出电梯+1; 然后考虑最高的点,当前最高的点即是电梯要到达的最高位置;
同理再考虑下降方向:假设当前区间为 [ l , r ] [l, r] [l,r], l > r l > r l>r, l l l处-1, r r r处+1.然后对所有点进行排序,先考虑最高的位置,大概思路就是这样,细节看代码吧。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
typedef struct node
{
int pos, type;
bool operator < (const struct node &w) const {
if(pos != w.pos) return pos > w.pos;
return type < w.type;
}
}Node;
vector<Node> up, down; // 分别存储上升和下降的乘客
int n, m, k;
signed main()
{
cin >> n >> m >> k;
for(int i = 0; i < n; i ++){
int a, b; cin >> a >> b;
if(a < b) {
up.push_back({
a, -1});
up.push_back({
b, 1});
}else{
down.push_back({
a, 1});
down.push_back({
b, -1});
}
}
vector<int> t1, t2; // 分别存储每一趟上升和下降的最大高度
sort(up.begin(), up.end());
sort(down.begin(), down.end());
int temp = 0;
for(auto item : up) {
temp += item.type;
if(temp > t1.size() * m) t1.push_back(item.pos);
}
temp = 0;
for(auto item : down) {
temp += item.type;
if(temp > t2.size() * m) t2.push_back(item.pos);
}
int res = 0;
for(int i = 0; i < t1.size() || i < t2.size(); i ++){
temp = 0;
if(i < t1.size()) temp = max(temp, t1[i]);
if(i < t2.size()) temp = max(temp, t2[i]);
res += (temp - 1) * 2;
}
cout << res << endl;
}
边栏推荐
- 网站最终产品页使用单一入口还是多入口?
- 软件测试面试题:手工测试与自动测试有哪些区别?
- [idea] idea configures sql formatting
- Chinese and Japanese color style
- 软件测试面试题:什么是软件测试?软件测试的目的与原则?
- Some thoughts on writing
- 2022杭电多校 第三场 B题 Boss Rush
- 【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
- 软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
- IDEA file encoding modification
猜你喜欢
![[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1](/img/8b/360df9a9094037dc358cb21c60cdc8.png)
[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1

Modelers experience sharing: model study method

【idea】idea配置sql格式化

【Valentine's Day special effects】--Canvas realizes full screen love

leetcode: 266. All Palindromic Permutations

国内网站用香港服务器会被封吗?

Getting started with 3D modeling for games, what modeling software can I choose?

标识符、关键字、常量 和变量(C语言)

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

怎样进行在不改变主线程执行的时候,进行日志的记录
随机推荐
典型相关分析CCA计算过程
Detailed explanation of common DNS resource record types
性能测试如何准备测试数据
【LeetCode】滑动窗口题解汇总
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
【云原生--Kubernetes】Pod控制器
仿网易云音乐小程序-uniapp
关于使用read table 语句
Brainstorm: Complete Backpack
What is next-generation modeling (with learning materials)
D - I Hate Non-integer Number (选数的计数dp
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
【unity编译器扩展之模型动画拷贝】
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
软件测试面试题:做好测试计划的关键是什么?
Some thoughts on writing
2022多校第二场 K题 Link with Bracket Sequence I
网站最终产品页使用单一入口还是多入口?
子连接中的参数传递
tiup telemetry