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;
}
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;
}
相关文章推荐
- 2016多校训练Contest10: 1003 Captain is coding hdu5859
- 2016多校训练Contest5: 1012 World is Exploding hdu5792
- 2016多校训练Contest5: 1004 How Many Triangles hdu5784
- 2016多校训练Contest5: 1011 Two hdu5791
- 2016第三场多校联盟训练1003
- 2016第三场多校联盟训练1003
- 2016多校训练Contest8: 1003 color II hdu5823
- 2016多校训练Contest5: 1001 ATM Mechine hdu5781
- 2016多校训练Contest10: 1011 Water problem hdu5867
- 2016多校联合训练4 F - Substring 后缀数组
- 2016多校训练Contest4: 1007 Treasure hdu5770
- 2016第三场多校联盟训练1010
- 2016多校训练Contest6: 1001 A Boring Question hdu5793
- 2016多校训练Contest10: 1001 Median hdu5857
- 2017多校训练Contest4: 1003 Counting Divisors hdu6069
- HDOJ 5734 (2016多校联合训练 Training Contest 2) Acperience
- [HDU5727] Necklace [2016 Multi-University Training Contest 1(2016多校联合训练1) E]
- HDOJ 5754 (2016多校联合训练 Training Contest 3) Life Winner Bo
- 多校联合训练2016---hdu5798Stabliztion
- HDOJ 5744 (2016多校联合训练 Training Contest 2) Keep On Movin