[HDOJ5783]Divide the Sequence(贪心)

时间:2023-03-09 13:03:24
[HDOJ5783]Divide the Sequence(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5783

题意:给n个数,要求划分成多个段,使得每一个段的任意前缀和都不小于0。

从后往前截取,这样不会影响到未截取的部分。维护当前的前缀和,每次截取不要忘记给前缀和置零。

 #include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int maxn = ;
int n, ret;
LL cur;
LL a[maxn]; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
ret = ;
for(int i = ; i <= n; i++) {
scanf("%I64d", &a[i]);
}
int i = n; cur = ;
while(i >= ) {
cur += a[i];
if(cur >= ) {
cur = ;
ret++;
}
i--;
}
printf("%d\n", ret);
}
return ;
}