2016多校5 hdu 5783 Divide the Sequence 贪心
来源:互联网 发布:英语网络主播 编辑:程序博客网 时间:2024/05/19 06:17
题目链接:这里
题意:说的是,给你n个数,让你分成最多的块,使得每一块的任何前缀,都是大于0的。问你最多分成多少块?
解法:把长度为n的序列分成尽量多的连续段,使得每一段的每个前缀和都不小于0。保证有解。 从后往前贪心分段即可。
//HDU 5783#include <bits/stdc++.h>using namespace std;const int maxn = 1e6+7;long long a[maxn];int n;int main(){ while(scanf("%d", &n) != EOF) { for(int i = 1; i <= n; i++) scanf("%lld", &a[i]); long long now = 0; int ans = 0, flag = 0; for(int i = n; i >= 1; i--){ if(flag == 0 && a[i] >= 0){ ans++; } else if(flag == 0 && a[i] < 0){ ans++; now += a[i]; flag = 1; } else if(flag == 1){ now += a[i]; if(now >= 0){ now = 0, flag = 0; } } } printf("%d\n", ans); } return 0;}
0 0
- 2016多校5 hdu 5783 Divide the Sequence 贪心
- HDU 5783 Divide the Sequence 【贪心】
- HDU 5783 Divide the Sequence(贪心)
- 【贪心】HDU 5783 Divide the Sequence
- HDU-5783 Divide the Sequence(贪心水题)
- HDU 5783 Divide the Sequence——贪心
- hdu 5783 Divide the Sequence
- Divide the Sequence hdu 5783
- hdu 5783 Divide the Sequence
- HDU 5783 Divide the Sequence
- 【HDU】5783 Divide the Sequence
- hdu 5783 Divide the Sequence
- HDU 5783 Divide the Sequence
- HDU 5783 Divide the Sequence
- HDOJ 5783 Divide the Sequence(贪心)
- 2016多校5 hdoj5783 Divide the Sequence
- HDU5783 Divide the Sequence(贪心)
- hdu 5783 Divide the Sequence【水题】
- Android中的Service:默默的奉献者 (1)
- 欢迎使用CSDN-markdown编辑器
- Java中的24种设计模式与7大原则
- springmvc前台向后台传值几种方式从简单到复杂
- String
- 2016多校5 hdu 5783 Divide the Sequence 贪心
- JAVA中的帮助类
- C#读取Excel五种方式体会(二)
- Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
- 移植uClinux内核到stm32f407
- 数字图像处理 颜色空间RGB、HSI、CMYK、YUV的相互转换
- [Thinking in Java]
- Android:用GSON 五招之内搞定任何JSON数组
- 含测试点 | 浅谈软件测试之静态测试