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. Java中LinkedList的remove方法真的耗时O(1)吗?

    这个问题其实来源于Leetcode的一道题目,也就是上一篇日志 LRU Cache.在使用LinkedList超时后,换成ArrayList居然AC了,而问题居然是在于List.remove(Obje ...

  2. C Primer Plus 第3章 数据和C 编程练习

    1. /* 整数上溢 */ #include <stdio.h> int main(void) { ; unsigned ; /* 无符号整数j像一个汽车里程指示表(形容的太好了,可参考& ...

  3. 小米2S 连接Ubuntu Android Studio

    1. 首先打开手机上的开发者选项,USB调试.拨号:*#*#717717#*#*  ,手机会以Toast形式出现“……enable”字样.再次拨号可disable. 2. Ubuntu安装mtpfs: ...

  4. IntelliJ IDEA 15开发Java Maven项目

    1.安装好之后开始创建项目

  5. 246. Strobogrammatic Number

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

  6. javascript grunt安装和使用

    grunt是javascript世界的构建工具. 为何要用构建工具? 一句话:自动化.对于需要反复重复的任务,例如压缩(minification).编译.单元测试.linting等.自动化工具可以减轻 ...

  7. getpeername

    定义: int getpeername(int s, struct sockaddr *name, socklen_t *namelen); 描述: 获取socket的对方地址   得到对方的地址 s ...

  8. NLP基本任务-nltk_data文本分割

    将文本分割为句子 nltk.sent_tokenize(text,language) text:需要分割的文本 language:语言种类 czech捷克语 danish丹麦语 dutch荷兰语 en ...

  9. 面试题_66_to_75_Java IO 和 NIO 的面试题

    IO 是 Java 面试中一个非常重要的点.你应该很好掌握 Java IO,NIO,NIO2 以及与操作系统,磁盘 IO 相关的基础知识.下面是 Java IO 中经常问的问题. 66)在我 Java ...

  10. 使用netcat进行反弹链接的shellcode

    from:http://morgawr.github.io/hacking/2014/03/29/shellcode-to-reverse-bind-with-netcat/ 这篇文章主要是谈,在远程 ...