Divide the Sequence

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 649    Accepted Submission(s): 331

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
 
 
 
解析:要使所有前缀和不小于0,只需从后向前扫描整个序列a[],如果a[i]非负,则a[i]是一个满足条件的子序列;否则,继续向前扫描,直到前缀和非负,形成一个满足条件的子序列。如此进行贪心即可。
 
 
 
#include <cstdio>
#define ll long long const int MAXN = 1e6+5;
int a[MAXN]; int main()
{
int n;
while(~scanf("%d", &n)){
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
ll sum = 0;
int ans = 0;
for(int i = n-1; i >= 0; --i){
sum += a[i];
if(sum >= 0){
++ans;
sum = 0;
}
}
printf("%d\n", ans);
}
return 0;
}

  

HDU 5783 Divide the Sequence的更多相关文章

  1. HDU 5783 Divide the Sequence(数列划分)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. HDU 5783 Divide the Sequence (贪心)

    Divide the Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...

  3. hdu 5783 Divide the Sequence 贪心

    Divide the Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...

  4. 【贪心】HDU 5783 Divide the Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 题目大意: 把一个N个数的数列拆成若干段,保证每一段的前缀和都非负,求最多能拆成多少段. 题目 ...

  5. HDU 5783 Divide the Sequence (训练题002 B)

    Description Alice has a sequence A, She wants to split A into as much as possible continuous subsequ ...

  6. 2016 Multi-University Training Contest 5 Divide the Sequence

    Divide the Sequence 题意: 给你一个序列A,问你最多能够分成多少个连续子序列,使得每个子序列的所有前缀和均不小于0 题解: 这题是比赛时候的水题,但我比的时候也就做出这一题, = ...

  7. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  8. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  9. Hdu 5496 Beauty of Sequence (组合数)

    题目链接: Hdu 5496 Beauty of Sequence 题目描述: 一个整数序列,除去连续的相同数字(保留一个)后,序列的和成为完美序列和.问:一个整数序列的所有子序列的完美序列和? 解题 ...

随机推荐

  1. JsRender系列demo(3)-自定义容器

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. Url重写和伪静态

    这里是URL重写的精华:http://msdn.microsoft.com/zh-cn/library/ms972974.aspx感觉写的非常棒. 其实URL重写操作起来也是挺简单的,只要你在前台写好 ...

  3. linux下获取时间差

    #include <sys/time.h> struct timeval tpstart,tpend;     float timeuse;     gettimeofday(&t ...

  4. DataTime.Now.Ticks的应用

    参考:http://www.cnblogs.com/delphinet/archive/2011/06/09/2075985.html 转换成秒: using System; using System ...

  5. js模块化开发

    主要有两个:一个是sea.js,另一个是require.js

  6. Unix环境编程之定时、信号与中断

    在linux下实现精度较高的定时功能,需要用到setitimer 和 getitimer函数. 函数原型: #include <sys/time.h> int getitimer(int ...

  7. 2014--9=17 软工二班 MyEclipse blue==5

    package cn.rwkj.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputS ...

  8. ehcache版本冲突

    以ehchache-core2.5为分水岭 缓存版本问题 版本不一样 配置不一样  ehcache-core-2.4.3.jar 与 ehcache-core-2.6.6 一 Caused by: n ...

  9. iOS方法封装

    (void) setSubView:(UIView *)masterView subCCGRect:(CGRect)subCCGRect imageName:(NSString *)imageName ...

  10. HDFS小文件处理——Mapper处理

    处理小文件的时候,可以通过org.apache.hadoop.io.SequenceFile.Writer类将所有文件写出到一个seq文件中. 大致流程如下: 实现代码: package study. ...