HDU 5783 Divide the Sequence

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5783

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,A2An.
1n1e6
10000A[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
 
Hint
题意:

给你n个数,让你分成最多的块,使得每一块的任何前缀,都是大于0的。

问你最多分成多少块。

题解:

用贪心的思想,从后往前就行。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e6+10;
#define ll long long int
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,cnt=0;
        for(ll i=n-1;i>=0;i--)
        {
            sum+=a[i];
            if(sum>=0)
            {
                sum=0;
                cnt++;
            }
        }
        printf("%d\n",cnt);
    }
}

  

 
posted @ 2016-08-02 20:48  _Silver  阅读(132)  评论(0编辑  收藏  举报