博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 5783 Divide the Sequence 贪心
阅读量:6915 次
发布时间:2019-06-27

本文共 1413 字,大约阅读时间需要 4 分钟。

Divide the Sequence

题目连接:

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
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<
<

转载地址:http://qbacl.baihongyu.com/

你可能感兴趣的文章
SVN的部署和仓库的备份及WIN7客户端测试
查看>>
机器学习的算法选择
查看>>
java:正则表达式 --转http://blog.csdn.net/yangjiali014/archive/2007/06/19/1658235.aspx
查看>>
RVM 安装与使用帮助
查看>>
Tomcat性能调优方案
查看>>
Ubuntu12.04上编译PlateGatewayQt
查看>>
(转)UITableView使用详解 相当详细,不错的东东
查看>>
Java中JDK,JRE和JVM之间的关系
查看>>
Python-NLTK环境搭建
查看>>
二叉搜索树转换成有序的双向链表
查看>>
sql获取每门课程成绩最好的学生信息
查看>>
入坑IT都快十年了
查看>>
《并行计算的编程模型》一3.7.1 选择集合参与者
查看>>
百分点:利用大数据做智慧商业
查看>>
浅析自动化设备安装运维的发展方向
查看>>
行为型模式:模板方法
查看>>
区块链:定义未来金融与经济新格局
查看>>
mongoDB高级查询这一篇就够了
查看>>
js节流和防抖
查看>>
VUE 使用笔记
查看>>