|
Divide the SequenceTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 962 Accepted Submission(s): 471
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,A2⋯An . 1≤n≤1e6 −10000≤A[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
Sample Output
Author
ZSTU
Source
|
水题水题,就是求最多能把一个数组分成几份,保证每份都连续而且每个前缀和都大于0。
思路:从后往前扫,每遇到一个负数就继续往前知道可以使得前缀和大于0。
看代码把。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int INF=1000000007;
const int MAXN =1000010;
const int MAXM=1000010;
long long a[MAXN];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++)
scanf("%I64d",a+i);
int cnt=0;
long long s=0;
for(int i=n;i>=1;i--){
if(a[i]>=0&&s==0)
cnt++;
else if(a[i]<0){
s+=a[i];
}
else if(a[i]>=0&&s!=0){
s+=a[i];
if(s>=0)
{
cnt++;
s=0;
}
}
}
printf("%d\n",cnt);
}
}