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

Divide the Sequence

2017-04-29 16:25 281 查看

Divide the Sequence

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

Total Submission(s): 1462    Accepted Submission(s): 693


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
For each test case, output an integer indicates the maximum number of sequence division.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

6
2
5

 

[align=left]Author[/align]
ZSTU
 

[align=left]Source[/align]
2016 Multi-University Training Contest 5
 

题意分析:把长度为n的序列分成尽量多的连续段,使得每一段的每个前缀和都不小于0。保证有解。

从后往前贪心分段即可。大于等于0的为一段,遇到负数就一直相加到非负为止!(注意精度问题 用long long)

AC代码:

#include<iostream>

#include<algorithm>

#include<stdio.h>

using namespace std;

typedef long long LL;

const int N=1000010;

int a
;

int main()

{

        LL n,i;

        while(scanf("%lld",&n)!=EOF)

        {

                for(i=1;i<=n;i++)

                        scanf("%lld",&a[i]);

 
4000
              LL sum=0;

                for(i=n;i>=1;i--)

                {

                        sum+=a[i];

                        if(sum>=0)

                                sum=0;

                        else n--;

                }

                printf("%lld\n",n);

        }

        return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: