HDU1003MAX SUM (动态规划求最大子序列的和)
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 206582 Accepted Submission(s):
48294
to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7),
the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
T(1<=T<=20) which means the number of test cases. Then T lines follow,
each line starts with a number N(1<=N<=100000), then N integers
followed(all the integers are between -1000 and 1000).
first line is "Case #:", # means the number of the test case. The second line
contains three integers, the Max Sum in the sequence, the start position of the
sub-sequence, the end position of the sub-sequence. If there are more than one
result, output the first one. Output a blank line between two cases.
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
14 1 4
Case 2:
7 1 6
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int a[maxn],n;
int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int sum=,ans=-;
int s=,e=,k=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
if(sum>ans)
{
s=k;
e=i;
ans=sum;
}
if(sum<) //0的意义就是这段数做的是负功
{
sum=;
k=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",ans,s,e);
if(t>) puts("");
}
return ;
}
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N],n;
int main()
{
int t,cas=;
cin>>t;
while(t--)
{
cin>>n;
int sum=,mx=-,s=,e=,ts=;
for(int i=;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
if(sum>mx)
{
mx=sum;
s=ts;
e=i;
}
if(sum<)
{
sum=;
ts=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",mx,s,e);
if(t) printf("\n");
}
return ;
}
/*
100
2 1 2
1 1
3 -1 1 2
2 -7 3
*/
牢记顺序是 加 大于 小于!!
HDU1003MAX SUM (动态规划求最大子序列的和)的更多相关文章
- HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1003 Max Sum * 最长递增子序列(求序列累加最大值)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU1003MAX SUM
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 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(最大子序列和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
1.问题描述 给定一个字符串(序列),求该序列的最长的回文子序列. 2.分析 需要理解的几个概念: ---回文 ---子序列 ---子串 http://www.cnblogs.com/LCCRNblo ...
随机推荐
- What to do when Enterprise Manager is not able to connect to the database instance (ORA-28001)
摘自:http://dbtricks.com/?p=34 If you are trying to connect to the Oracle enterprise Manger and you ge ...
- 第二篇 Python初识别及变量名定义规范
第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是 .py 两种python程 ...
- Python全栈 MongoDB 数据库(数据的查找)
非关系型数据库和关系型数据库的区别? 不是以关系模型构建的,结构自由 非关系型数据库不保证数据一致性 非关系型数据库可以在处理高并发和海量数据时弥补关系数据库的不足 非关系型数据库在技术上没有关系 ...
- 梳理 Opengl ES 3.0 (二)剖析一个GLSL程序
OpenGL ES shading language 3.0 也被称作 GLSL,是个 C风格的编程语言. Opengl ES 3.0内部有两种可编程处理单元,即Vertex processor和Fr ...
- 数据挖掘算法:DBSCAN算法的C++实现
(期末考试快到了,所以比较粗糙,请各位读者理解..) 一. 概念 DBSCAN是一种产生划分聚类的基于密度的聚类算法,簇的个数由算法自动地确定.低密度区域中的点被视为噪声而忽略,因此DBSCAN ...
- Python 3 学习笔记之——错误和异常
1. 语法错误 Python 的语法错误被称为解析错,语法分析器会指出出错的代码行,并且在最先找到的错误的位置标记一个小小的箭头. >>> while True File " ...
- SpringBoot:工厂模式实现定时任务可配置
pringBoot:工厂模式实现定时任务可配置 需要:使用springboot,实现定时任务可配置. 定时任务可在代码中写死,在配置文件中配置,这些都不能实现定时任务在服务器不重启的情况下可配置. 为 ...
- Python图像全屏显示
需要在嵌入式设备上全屏显示图像,使用pil显示图像时,只能通过系统的图像浏览器显示.所以使用Python自带的tkinter import Tkinter as tk 这句在Python3中已经改 ...
- lintcode-130-堆化
130-堆化 给出一个整数数组,堆化操作就是把它变成一个最小堆数组. 对于堆数组A,A[0]是堆的根,并对于每个A[i],A [i * 2 + 1]是A[i]的左儿子并且A[i * 2 + 2]是A[ ...
- Struts1之logic标签
logic是Struts1中的逻辑标签 <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-l ...