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 DescriptionAlice 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.
InputThe 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.
OutputFor each test case, output an integer indicates the maximum number of sequence division.
Sample Input6 1 2 3 4 5 6 4 1 2 -3 0 5 0 0 0 0 0
Sample Output6 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 }