Divide the Sequence

来源:互联网 发布:橡胶木家具 价格算法 编辑:程序博客网 时间:2024/05/10 06:05

Divide the Sequence

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 70    Accepted Submission(s): 41


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
61 2 3 4 5 641 2 -3 050 0 0 0 0
 

Sample Output
625
把长度为n的序列分成尽量多的连续段,使得每一段的每个前缀和都不小于0。保证有解。 从后往前贪心分段即可。
#include<stdio.h>#include<iostream>using namespace std;#include<string.h>#define maxn 10000long long a[1000020];int main(){    int n,m,k;    while(scanf("%d",&n)!=-1)    {        for(int i=0; i<n; i++)            scanf("%lld",&a[i]);        int sum=0;        for(int i=n-1; i>=0; i--)        {            if(a[i]>=0)            {                sum++;            }            else if(a[i]<0)            {                a[i-1]=a[i-1]+a[i];            }        }        printf("%d\n",sum);    }}

 
0 0
原创粉丝点击