[51NOD1959]循环数组最大子段和(dp,思路)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050
这道题的最大子段和有两种可能,一种是常规的子段和,另一种是从结尾到开头的一个子段。常规做是一种可能,另一种带循环的则可以认为是序列中间有一段最小子段和,把这段最小子段和去掉,剩下的可能就是最大子段和了。
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int maxn = * ;
int n;
int a[maxn]; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
bool flag = ;
LL sum = , ret1 = , ret2 = ;
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
sum += a[i];
if(a[i] < ) flag = ;
}
if(!flag) {
printf("%lld\n", sum);
continue;
}
LL tmp = ;
for(int i = ; i <= n; i++) {
ret1 = max(tmp, ret1);
tmp += a[i];
if(tmp < ) tmp = ;
}
for(int i = ; i <= n; i++) a[i] = -a[i];
tmp = ;
for(int i = ; i <= n; i++) {
ret2 = max(tmp, ret2);
tmp += a[i];
if(tmp < ) tmp = ;
}
printf("%lld\n", max(ret1, sum+ret2));
}
return ;
}
[51NOD1959]循环数组最大子段和(dp,思路)的更多相关文章
- 51Nod 1050 循环数组最大子段和 | DP
Input示例 6 -2 11 -4 13 -5 -2 Output示例 20 分析: 有两种可能,第一种为正常从[1 - n]序列中的最大子字段和:第二种为数组的total_sum - ([1-n] ...
- 51nod 1050 循环数组最大子段和【环形DP/最大子段和/正难则反】
1050 循环数组最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该 ...
- 1042 数字0-9的数量 1050 循环数组最大子段和 1062 序列中最大的数 1067 Bash游戏 V2 1092 回文字符串
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次 ...
- 51nod 循环数组最大子段和(动态规划)
循环数组最大子段和 输入 第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9) 输出 输 ...
- 51nod 1050 循环数组最大子段和
题目链接:51nod 1050 循环数组最大子段和 #include<stdio.h> #include<algorithm> using namespace std; ; l ...
- 51nod 1050 循环数组最大子段和【动态规划】
N个整数组成的循环序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的连续的子段和的最大值(循环序列是指n个数围成一个圈,因此需要考虑a[n-1],a[n] ...
- 51nod 1050 循环数组最大子段和 单调队列优化DP
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 这个呢,这个题之前 求一遍最大值 然后求一遍最小值 ...
- 51nod 1050 循环数组最大子段和 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1050¬iceId=13385 参考:http://blog. ...
- 51nod 循环数组最大子段和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 对于普通的数组,只要求一次最大子段和即可.但是这题是可以循环的,所 ...
随机推荐
- Openstack的配额共功能的使用
在一个云系统中,一个项目不能无限制的使用资源,必须对项目进行配额管理,在openstack中主要的命令是nova quota-update, 但是可能会提示的错误: DEBUG (shell:740) ...
- Date() 及其 如何验证用户输入的日期是合法的
1.var someDate = new Date(Date.parse("May 25, 2004")); <=> var someDate = new Dat ...
- 【python cookbook】【数据结构与算法】10.从序列中移除重复项且保持元素间顺序不变
问题:从序列中移除重复的元素,但仍然保持剩下的元素顺序不变 解决方案: 1.如果序列中的值时可哈希(hashable)的,可以通过使用集合和生成器解决.
- Cacti Install
一.Cacti简介 Cacti是通过snmpget来获取数据,使用RRDtool绘画图形,而且你完全可以不需要了解RRDtool复杂的参数.它提供了非常强大的数据和用户管理功能,可以指定每一个用户能查 ...
- Intent 转向
Intent intent = new Intent(CardInfoActivity.this, CardRechargeListActivity.class); intent.putExtra(& ...
- Greenplum迁移到配置不同的GP系统
要使用gp_restore或gpdbrestore并行恢复操作,恢复的系统必须与备份的系统具有相同的配置(相同数量的Instance).如果想要恢复数据库对象和数据到配置不同的系统(比如系统扩展了更多 ...
- python: html
1. 三把利剑(html css js) css(颜色 位置) js (动) 2. 标签的分类:块级标签和行内标签 块级标签(div h p) 行内标签(span) 3. 标签存在的意义:为了方便操作 ...
- java poi Excel导入 整数浮点数转换问题解决
/** * 获取单元格数据 */ protected static String getCellValue(Cell cell) { String cellValu ...
- 实现Fragment的切换和ViewPager自动循环设置切换时间
1.FragmentActivity与Fragment之间的用法 2.实现ViewPager自动轮换,设置移动的时间 通过反射获取mScrooler这个对象: Field mScroller; mSc ...
- 越狱后如何添加cydia源及cydia源大全
http://jingyan.baidu.com/article/6c67b1d6cc53fe2787bb1e06.html 软件游戏能PJ,插件其实也一样,所以有些源里面就包含了大量的PJ插件~ a ...