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 A_{1},A_{2}\cdots A_{n}
.
1 \leq n \leq 1e6
-10000 \leq A[i] \leq 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 就是给你一个序列,让你求最多能分成多少段连续子序列,并且每段子序列的前缀个都得是大于等于0的;思路就是从后往前遍历,要想分的最多那么有整数直接,当成一个数列就行了,有负数往前递加直到和为零。这里特别注意,数据要用long long要不然会溢出的 #include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#define N 1000005#define M (1<<15)+5#define INF 0x3f3f3fusing namespace std;long long n,a[N],sum;int main(){ //freopen("in.txt","r",stdin); while(scanf("%lld",&n)!=EOF) { memset(a,0,sizeof(a)); for(int i=0;i<n;i++) { scanf("%lld",&a[i]); } long long cur=0; for(int i=n-1;i>=0;i--)//这个是首位枚举 { if(a[i]>=0) cur++; else { sum=0; for(;i>=0;i--) { sum+=a[i]; if(sum>=0) { cur++; break; } } } } printf("%lld\n",cur); } return 0;}
时间: 2024-12-19 22:25:20