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

2016多校训练Contest5: 1003 Divide the Sequence hdu5783

2016-08-02 23:07 369 查看
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

直接从后往前贪心即可

队友写的代码

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int INF = 2147483647;
const double PI = acos(-1);

/* ----------------- code ----------------- */

long long a[1000100],sum[1000100];
int main(void){

int n;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sum[0] = 0;
sum[1] = a[1];
for(int i=2;i<=n;i++){
sum[i] = sum[i-1] + a[i];
}
int ans = 0;
for(int i=n;i>=1;i--){
for(int j=0;j<=n;j++){
int p = i - j;
if(p<1) break;
if(sum[i]-sum[p-1] < 0) continue;
else{
//printf("%d %d\n",p,i);
i = p;
ans++;
break;
}
}
}
printf("%d\n",ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心