1 前缀和练习

1)跳楼机人数安排

题目

点击查看代码
中文解释#include <bits/stdc++.h>

using namespace std;

#define int long long

#define dbg(x) cout << #x << '=' << x << endl

const int N = 2e5 + 15;

int n, m, q[N], a[N];

signed main()
{
ios::sync_with_stdio (false); cin.tie (nullptr);

cin >> n >> m;

for (int i = 1; i <= n; i++) cin >> q[i];
for (int i = 1; i <= n; i++) cin >> a[i];

for (int i = 1; i <= n; i++) q[i] = q[i - 1] + q[i];

int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += a[i];
int u = lower_bound (q + 1, q + 1 + n, sum) - q;//这里是-q.第一个大于等于

if (q[u] == sum) u++;//u仓非空

if (u <= n) cout << n - u + 1 << '\n';
else 
{
    cout << n << '\n';
    sum = 0;
} 
}

return 0;
}  
posted on 2024-08-13 16:25  Hoshino1  阅读(3)  评论(0编辑  收藏  举报