题解:P15718 [JAG 2023 Summer Camp #3] Roller Coaster

· · 题解

P15718 [JAG 2023 Summer Camp #3] Roller Coaster 题解

::::info[化简题目] 有 n 个组排队乘坐过山车,第 i 组有 a_i 个人。

过山车每分钟发一班,每班最多能载 m 人。

规则如下:

  1. 同一组的人必须同乘一班车;
  2. 只要当前班次能装下整个组,就乘坐当前班次;
  3. 每组都想尽快乘车。
最后输出每个组的乘车时间。
::::info[思路分析]
我们可以模拟乘车过程,设当前班次时间为 cnt,已载人数为 ans。

再遍历数组:

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;

}



::::
::::warning[恳求]
求管理员审核通过,求各位点赞评论加关注再走吧qwq
::::