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

HDOJ 5783 (2016多校联合训练 Training Contest 5) Divide the Sequence_i++

本来一道可以出的很难的题,但是题目给了一个条件让这题变得很水。

题目保证了有解,也就是说我们不用考虑会有无解的情况,那么我们只需要直接从后面开始讨论每一位的后缀和,只要大于0就可以算成一个序列。


#include <cstdio>
const int maxn = 1000000+5;
typedef long long LL;
int a[maxn];
int main()
{
int n;
while(scanf("%d", &n) !=EOF)
{
for(int i=0; i<n; i++) scanf("%d", &a[i]);
LL sum = 0, ans = 0;
for(int i=n-1; i>=0; i--)
{
sum += a[i];
if(sum >= 0) ans++, sum = 0;
}
printf("%I64d\n", ans);
}
}