hdu 5783 Divide the Sequence 贪心
Divide the Sequence
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5783
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
Hint
题意
说的是,给你n个数,让你分成最多的块,使得每一块的任何前缀,都是大于0的。
问你最多分成多少块
题解:
把长度为n的序列分成尽量多的连续段,使得每一段的每个前缀和都不小于0。保证有解。 从后往前贪心分段即可。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n;
long long a[maxn];
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
}
long long flag = 0;
long long ans = 0;
long long now = 0;
for(int i=n;i>=1;i--){
if(flag==0&&a[i]>=0){
ans++;
flag = 0;
}
else if(flag==0&&a[i]<0){
flag=1;
now+=a[i];
ans++;
}else if(flag==1){
now+=a[i];
if(now>=0){
now=0,flag=0;
}
}
}
cout<<ans<<endl;
}
}
hdu 5783 Divide the Sequence 贪心的更多相关文章
- HDU 5783 Divide the Sequence(数列划分)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5783 Divide the Sequence (贪心)
Divide the Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...
- HDU 5783 Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 【贪心】HDU 5783 Divide the Sequence
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 题目大意: 把一个N个数的数列拆成若干段,保证每一段的前缀和都非负,求最多能拆成多少段. 题目 ...
- 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 ...
- Divide the Sequence (贪心)
题意:求将一串数据尽可能多分成所有前缀和大于0的连续子串. 思路:由于是要求所有前缀和大于0,那么只要从后往前推就好了. #include<bits/stdc++.h> using nam ...
- 2016 Multi-University Training Contest 5 Divide the Sequence
Divide the Sequence 题意: 给你一个序列A,问你最多能够分成多少个连续子序列,使得每个子序列的所有前缀和均不小于0 题解: 这题是比赛时候的水题,但我比的时候也就做出这一题, = ...
- 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence
// 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...
- HDU5014Number Sequence(贪心)
HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...
随机推荐
- C根据排序字符串
#include<stdio.h> #include<string.h> #include <stdlib.h> #define STR_LEN_MAX 100 c ...
- 第9月第12天 lua_push lua_to luaL_check stack quick
1. c代码中通过lua_push 把数据压入堆栈,lua调用c函数得到数据.luaL_check是对lua_to的封装,从堆栈中获取lua代码中函数调用的数据. static int lread(l ...
- deeplearning.ai学习seq2seq模型
一.seq2seq架构图 seq2seq模型左边绿色的部分我们称之为encoder,左边的循环输入最终生成一个固定向量作为右侧的输入,右边紫色的部分我们称之为decoder.单看右侧这个结构跟我们之前 ...
- deeplearning.ai学习LSTM
一.LSTM架构与公式 这里的a<t>表示的就是原始输出,y<t>表示的就是经过softmax的输出,c<t>相当于状态.u(update)代表是输入门,f代表遗忘 ...
- 简单解读linux的/proc下的statm、maps、memmap 内存信息文件分析【转】
转自:https://blog.csdn.net/sctq8888/article/details/7398776 转载自:http://hi.baidu.com/deep_pro/blog/item ...
- Newtonsoft 反序列化字符串
string json=“[{“name”:”zhangsan”,”age”:”12”},{“name”:”zhangsan”,”age”:”12”}]” 方法1: JArray arr = (JAr ...
- Ibatis.Net 执行存储过程学习(八)
首先在数据库创建存储过程: create proc [dbo].[usp_GetPersonById] @Id int as begin select Id,Name from Person wher ...
- dubbo启动报错failed to bind nettyserver on
dubbo报错 今天启动项目的时候,关掉了custom服务, <dubbo:consumer check="false"/> 并且关掉了spring的elastic-j ...
- laravel 5.1 Model 属性详解
<?php namespace Illuminate\Database\Eloquent; /** * 下面提到某些词的含义: * 1.覆盖: 在继承该类 \Illuminate\Databas ...
- CentOS 安装codeblocks
1. 安装wxWidgets .tar.bz2 [root@luozhonghua codeblocks]# cd wxWidgets- [root@luozhonghua wxWidgets-]# ...