Divide the Sequence

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

 

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

 

 

题意:

给定一串数字分成若干份,使任意份的任意前缀和都不为负。

 

从后向前遍历贪心。

 

附AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int a[1000010];
 9 
10 int main(){
11     int n;
12     while(~scanf("%d",&n)){
13         int sum=n-1;
14         for(int i=0;i<n;i++){
15             scanf("%d",&a[i]);
16         }
17         int t=0;
18         while(sum>=0){
19             long long ans=0;
20             ans+=a[sum];
21             while(ans<0){
22                 sum--;
23                 ans+=a[sum];
24             }
25             t++;
26             sum--;
27         }
28         printf("%I64d\n",t);
29 }
30     return 0;
31 }

 

posted @ 2016-08-07 09:12  Kiven#5197  阅读(297)  评论(0编辑  收藏  举报