您的位置:首页 > 产品设计 > UI/UE

hdu 5783 Divide the Sequence(2016 Multi-University Training Contest 5——水题)

2016-08-04 11:43 459 查看
题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=5783


Divide the Sequence

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 754    Accepted Submission(s): 396

Problem Description

Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfying that for each subsequence, every its prefix sum is not small than 0.

 

Input

The input consists of multiple test cases. 

Each test case begin with an integer n in a single line.

The next line contains n integers A1,A2⋯An.
1≤n≤1e6
−10000≤A[i]≤10000

You can assume that there is at least one solution.

 

Output

For each test case, output an integer indicates the maximum number of sequence division.

 

Sample Input

6
1 2 3 4 5 6
4
1 2 -3 0
5
0 0 0 0 0

 

Sample Output

6
2
5

 

Author

ZSTU

 

Source

2016 Multi-University Training Contest 5

 
题目大意:给一个序列,要尽可能多的分成很多子序列。使得每个子序列的前缀和都大于等于0,最后输出符合条件的子序列个数。
解题思路:
倒序遍历一遍序列,如果这个数大于等于0,就是符合条件的序列,否则就往前加,直到加到大于等于0即可符合条件。

详见代码。
#include <iostream>
#include <cstdio>

using namespace std;
#define ll long long

ll a[1000010];
int main()
{
int n;
ll sum,s;
while (~scanf("%d",&n))
{
sum=s=0;
for (int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
}
for (int i=n; i>=1; i--)
{
sum+=a[i];
//cout<<sum<<endl;
if (sum>=0)
{
s++;
sum=0;
}
}
printf ("%lld\n",s);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐