[ABC339C] Perfect Bus 题解
说句闲话:
ABC 的 C 题好久没这么水了,赛时 AC。
题目大意:
公共汽车上的乘客人数始终是一个非负整数。
在第
分析:
题目给出了每个车站上下车的人数,求最后最小的乘客数量。采用贪心策略。由于每时每刻的人数都应该是非负数,我们每次循环加上
Code :
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 10;
LL a[N];
int main() {
LL n; scanf("%lld", &n);
for (int i = 1; i <= n; ++i)
scanf("%lld", &a[i]);
LL ans = 0;
for (int i = 1; i <= n; ++i) {
if(ans + a[i] < 0) {
ans = 0;
//小于0就清0
} else ans += a[i];
//加上每次的上下车人数
}
printf("%lld", ans);
return 0;
}