hdu1003 Max Sum(最大子串)
https://vjudge.net/problem/HDU-1003
注意考虑如果全为负的情况,特判。
还有输出格式,最后一个输出不用再空行。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 1e9
typedef long long ll;
using namespace std;
int n, m, a[];
int main()
{
IO;
cin >> n;
int kase=;
while(n--){
cin >> m;
for(int i = ; i < m; i++){
cin >> a[i];
}
int sum = , maxm = -INF;
int from = ;
int ans1 = , ans2 = -;
for(int i = ; i < m; i++){
sum += a[i];
if(sum < ){
sum = ;
from = i+;
}
else{
if(sum > maxm){
maxm = sum;
ans1 = from;
ans2 = i;
}
}
}
cout << "Case " << ++kase << ":" << endl;
if(ans2 == -){//sum一直小于零,每位<0
maxm = -INF;
for(int i = ; i < m; i++){
if(a[i] > maxm){
maxm = a[i];
ans1 = i; ans2 = i;
}
}
cout << maxm << " " << ans1+ << " " << ans2+ << endl;
}
else
cout << maxm << " " << ans1+ << " " << ans2+ << endl;
if(n != ) cout << endl;
}
return ;
}
hdu1003 Max Sum(最大子串)的更多相关文章
- 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾
2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...
- [ACM_动态规划] hdu1003 Max Sum [最大连续子串和]
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)
最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...
- HDU1003 Max Sum(求最大字段和)
事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...
- hdu1003 Max Sum(经典dp )
A - 最大子段和 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descr ...
- HDU--1003 Max Sum(最大连续子序列和)
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- HDU-1003 Max Sum(动态规划,最长字段和问题)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)
Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...
- hdu1003 Max Sum【最大连续子序列之和】
题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...
随机推荐
- win7自带截屏便签 打开命令
win7自带截屏 1.win+r 2.SnippingTool.exe 打开 便签 1.win+r 2.StikyNot.exe 打开 查本机ip 1.win+r 2.cmd 3.ipco ...
- Flink--Split和select
Split就是将一个DataStream分成两个或者多个DataStream Select就是获取分流后对应的数据 val env = StreamExecutionEnvironment.getEx ...
- LVM分区无损增减
http://www.361way.com/change-lvm-size/1792.html
- Codeforces 609F Frogs and mosquitoes 线段树
Frogs and mosquitoes 用线段树维护每个点覆盖的最小id, 用multiset维护没有吃的蚊子. #include<bits/stdc++.h> #define LL l ...
- 51Nod1863 Travel 主席树 最短路 Dijkstra 哈希
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1863.html 题目传送门 - 51Nod1863 题意 有 n 个城市,有 m 条双向路径连通它们 ...
- Shell工具| 流程控制
1. 流程控制 if 判断 ()[ 条件判断式 ],中括号和条件判断式之间必须有空格 ()if后要有空格 [kris@hadoop datas]$ vim if.sh #!/bin/bash -eq ...
- 20165319 2017-2018-2《Java程序设计》课程总结
一.每周作业链接汇总 预备作业一:我期望的师生关系 20165319 我所期望的师生关系 预备作业二:学习基础和C语言基础调查 20165319 学习基础和C语言基础调查 摘要: 技能学习经验 c语言 ...
- String.IsNullorEmpty()方法的使用
!= null 就是不为null !string.IsNullOrEmpty 不是null且不是""(string.Empty)
- waf python build 工具使用流程
waf python build 工具使用流程 waf 的 build 理念 build 了之后,可以跟踪到 ${SRC} 和 ${TGT} 有关联的文件,只有 ${SRC} 被修改过,在下次buil ...
- GRNN/PNN:基于GRNN、PNN两神经网络实现并比较鸢尾花种类识别正确率、各个模型运行时间对比—Jason niu
load iris_data.mat P_train = []; T_train = []; P_test = []; T_test = []; for i = 1:3 temp_input = fe ...