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 ...
随机推荐
- 【CSS3】边框
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Libevent源码分析 (1) hello-world
Libevent源码分析 (1) hello-world ⑨月份接触了久闻大名的libevent,当时想读读源码,可是由于事情比较多一直没有时间,现在手头的东西基本告一段落了,我准备读读libeven ...
- 闲来无事做了一个批处理的win10账号管理
@echo off %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...
- IT小白学习Discuz!框架(一)
1.Discuz!是什么? 答:(1).Crossday Discuz! Board(简称 Discuz!)是北京康盛新创科技有限责任公司推出的一套通用的社区论坛软件系统. (2).Crossday ...
- js控制图片自动缩放,实现铺满盒子,不变形,完全局中
此js一般用于控制图片铺满盒子,但是比例不变,并且绝对局中原理:判断图片的高宽与盒子高宽的大小的关系,然后通过比例来控制图片的缩放及定位<!DOCTYPE html PUBLIC "- ...
- linux shadowsocket 安装和启动
http://blog.csdn.net/hanshileiai/article/details/49302865
- Qt创建停靠悬浮窗口
1.Qt实现窗口停靠和悬浮使用类QDockWidget,它有两个重要方法用来设置停靠特性以及停靠区域, dw1->setFeatures(QDockWidget::DockWidgetMovab ...
- web打印总结
一.打印样式 区别显示和打印的样式 使用media="print"控制打印时的样式,如下: 打印时不显示打印按钮,设置页面宽度 <style media="prin ...
- 微信小程序之使用本地接口开发
本文主要讲解如何使用本地接口进行开发,很多人都会遇到这个问题,特别是小程序上线后. 一.解决思路 在小程序开发工具设置网络代理,然后再通过Charles设置代理,将https域名转为本地接口进行访问. ...
- iOS微信运动 刷分
修改 iOS微信运动的数据 很简单,这里记录下实现步骤. 首先要安装Theos,具体安装步骤就不说了.网上很多. 大体安装步骤: sudo brew install dpkg sudo brew i ...