P15718 [JAG 2023 Summer Camp #3] Roller Coaster 题解
化简题目
有 个组排队乘坐过山车,第 组有 个人。
过山车每分钟发一班,每班最多能载 人。
规则如下:
- 同一组的人必须同乘一班车;
- 只要当前班次能装下整个组,就乘坐当前班次;
- 每组都想尽快乘车。
最后输出每个组的乘车时间。
思路分析
我们可以模拟乘车过程,设当前班次时间为 ,已载人数为 。
再遍历数组:
- 如果 的话,该组就上当前的班次,输出 , 就等于 ;
- 否则开启新班次 加 ,再输出 ,接着 就等于 。
代码实现
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int n, m;
int a[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
int cnt = 0, ans = 0; //cnt为当前班次时间,ans为当前班次已载人数
for(int i = 1; i <= n; i++){
if(ans + a[i] <= m){ //当前班次能装下
cout << cnt << endl;
ans += a[i];
}
else{ //当前班次装不下,要等下一班
cout << ++cnt << endl;
ans = a[i];
}
}
return 0;
}
恳求
求管理员审核通过,求各位点赞评论加关注再走吧qwq