您的位置:首页 > 产品设计 > UI/UE

HDU5783(2016多校第五场)——Divide the Sequence(水水水)

2016-08-07 16:48 447 查看
New~ 欢迎参加2016多校联合训练的同学们~ 

Divide the Sequence

Time 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

6
1 2 3 4 5 6
4
1 2 -3 0
5
0 0 0 0 0

 

Sample Output

6
2
5

 

Author

ZSTU
 

Source

2016 Multi-University Training Contest 5
 
水题水题,就是求最多能把一个数组分成几份,保证每份都连续而且每个前缀和都大于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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: