HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 250714 Accepted Submission(s): 59365
a sequence a[1],a[2],a[3]......a[n], your job is 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.
first line of the input contains an integer 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).
each test case, you should output two lines. The 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.
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Case 1:
14 1 4 Case 2:
7 1 6
#include <iostream>
using namespace std;
int main()
{
int j,i,k,n,m,t;
int a[];
scanf("%d",&t);
for (j=;j<=t;j++)
{
scanf("%d",&n);
for (i=;i<n;i++)
{
scanf("%d",&a[i]);
}
int sum=,maxsum=-,first =, last = , temp = ;
for (i=;i<n;i++)
{
sum += a[i];
if (sum > maxsum)
{
maxsum = sum;first = temp;last = i+;
}
if (sum < )
{
sum = ;temp = i+;
}
} printf("Case %d:\n%d %d %d\n",j,maxsum,first,last);
if (j!=t)
{
printf("\n");
}
} return ;
}
#include <iostream>
using namespace std;
int main()
{
int j,i,k,n,m,t;
int a; //不需要数组,只需要一个输入变量
scanf("%d",&t);
for (j=;j<=t;j++)
{
scanf("%d",&n);
int sum=,maxsum=-,first =, last = , temp = ;
for (i=;i<n;i++)
{
scanf("%d",&a);
sum += a;
if (sum > maxsum)
{
maxsum = sum;first = temp;last = i+;
}
if (sum < )
{
sum = ;temp = i+;
}
}
//注意格式,我就因为将冒号写到了数的前边而wrong answer,郁闷半天才发现……
printf("Case %d:\n%d %d %d\n",j,maxsum,first,last);
if (j!=t)
{
printf("\n");
}
} return ;
}
HDU 1003 Max 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 1003 Max Sum(动态规划)
解题思路: 本题在给定的集合中找到最大的子集合[子集合:集合的元素的总和,是所有子集合中的最大解.] 结果输出: 最大的子集合的所有元素的和,子集合在集合中的范围区间. 依次对元素相加,存到一个 su ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- hdu 1003 Max Sum (DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1003 Max Sum (动态规划)
转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...
- HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
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 Su ...
随机推荐
- 谷歌浏览器插件-jsonView插件安装与使用
本文转载:http://www.bubuko.com/infodetail-700647.html 1 安装 1.打开 https://github.com : 2.搜索 jsonView 链接:ht ...
- C#Lambda表达式Aggregate的用法及内部运行方式的猜想
, , , , }; // 其和为15 var count = nums.Aggregate((body, next) => { // 注意,nums的元素个数至少一个以上(但如果是有seed的 ...
- 探索C++对象模型
只说C++对象模型在内存中如何分配这是不现实的,所以这里选择VS 2013作为调试环境具体探讨object在内存中分配情况.目录给出了具体要探讨的所有模型,正文分标题依次讨论.水平有限,如有错误之处请 ...
- C:函数:功能:实现字符数组中所有字母的倒序存放并输出
前两天小测碰到一道题,建立一个函数,功能:实现字符数组中所有字母的倒序存放并输出,一开始觉得简单跟数字数组差不多,运行一下发现很多格式错误,这些是不必要的错误,现在就来说下,先说一下代码思路:定义一个 ...
- Python学习(二):函数入门
1.函数代码格式: def 函数名(): 函数内容 执行函数:函数名() 2.代码举例: #!/usr/bin/env python #coding=utf-8 #定义函数 def Func1(): ...
- tee 命令详解
作用:将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin . 简单的说就是把数据重定向给文件和屏幕上. 注意:存在缓存机制,每1024 字节输出一次, 若从管道接受数据 ...
- Mysql5.7.20 On Windows安装指导
安装环境 Windows版本:Windows10 64bit MySQL版本: MySQL5.7.20 配置过程 1.下载MySQL Community Server (下载链接) 根据自己操作系统需 ...
- 2017双11海量数据下EagleEye的使命和挑战
摘要: EagleEye作为阿里集团老牌的链路跟踪系统,其自身业务虽不在交易链路上,但却监控着全集团的链路状态,特别是在中间件的远程调用上,覆盖了集团绝大部分的场景,在问题排查和定位上发挥着巨大的作用 ...
- CSS3媒体查询(Media Queries)介绍
媒体类型 all 所有设备 screen 电脑显示器 handheld 便携设备 tv 电视类型设备 print 打印用纸打印预览视图 关键字 and not(排除某种设备) only(限定某种设备) ...
- Java Web高级编程(三)
使用过滤器改进应用程序 一.过滤器的目的 过滤器是可以拦截访问资源的请求.资源的响应或者同时拦截两者的应用组件.过滤器可以检测和修改请求和响应,同时也可以拒绝.重定向或转发请求.javax.servl ...