题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 158421    Accepted Submission(s):
37055

Problem Description
Given 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.
 
Input
The 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).
 
Output
For 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.
 
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
 
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
 
题目大意:给一串数字,然后要求出和最大的连续子序列。并且题目要求这个和最大的连续子序列输出起始位置和终止位置。
注意:初始化问题,还有空格的这个格式问题!
 
详见代码。
 #include <iostream>
#include <cstdio> using namespace std; struct node
{
int s,e,smax;
} sum[]; int main ()
{
int t,n,Max,k,j;
int num[];
while (~scanf("%d",&t))
{
int flag=;
while (t--)
{ scanf("%d",&n);
for (int i=; i<n; i++)
{
scanf("%d",&num[i]);
}
sum[].smax=num[];
sum[].s=sum[].e=;
k=,j=;
Max=sum[].smax;
for (int i=; i<n; i++)
{
if (num[i]>sum[i-].smax+num[i])
{
sum[i].smax=num[i];
sum[i].s=i;
sum[i].e=i;
}
else
{
sum[i].smax=sum[i-].smax+num[i];
sum[i].s=sum[i-].s;
sum[i].e=i;
}
if (Max<sum[i].smax)
{
Max=sum[i].smax;
k=sum[i].s;
j=sum[i].e;
}
}
printf ("Case %d:\n",flag++);
printf ("%d %d %d\n",Max,k+,j+);
if (t)
printf ("\n");
}
}
return ;
}
 
 

hdu 1003 Max Sum (DP)的更多相关文章

  1. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  4. hdu 1003 Max sum(简单DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem ...

  5. 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 ...

  6. HDU 1003 Max Sum(DP)

    点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...

  7. hdu 1003 Max Sum(基础dp)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. HDU 1003 Max Sum (动规)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

随机推荐

  1. 最近面试js部分试题总结

    二,JavaScript面试题总结 1,首先是数组去重算法:给一个数组,去掉重复值 (function() { var arr = [1, 2, 3, 3, 4, ]; function unique ...

  2. Qt MetaObject System详解

    网上的资源比较乱,该文章整理自地址:http://www.xuebuyuan.com/735789.html Qt meta-object系统基于三个方面: 1.QObject提供一个基类,方便派生类 ...

  3. ifstat查看网络流量的原理

    ifstat查看网卡流量的原理:读的是哪个/proc/ 接口啊 同diskIO一样,网络的IO也同样有统计计数的,是/proc/net/dev一个典型的输出就是这个样子的: root@station6 ...

  4. Ansible批量部署工具的安装

    1.系统安装gcc,以及python2.6以上(2.6.8): 第一种比较简单的安装方法: 1)直接yum install -y ansible; 2)然后更改配置,/etc/ansible/ansi ...

  5. BZOJ4552:[HEOI2016/TJOI2016]排序——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4552 https://www.luogu.org/problemnew/show/P2824 在2 ...

  6. BZOJ2038:[2009国家集训队]小Z的袜子——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找 ...

  7. HDOJ(HDU).2159 FATE (DP 带个数限制的完全背包)

    HDOJ(HDU).2159 FATE (DP 带个数限制的完全背包) 题意分析 与普通的完全背包大同小异,区别就在于多了一个个数限制,那么在普通的完全背包的基础上,增加一维,表示个数.同时for循环 ...

  8. oracle行转列函数WMSYS.WM_CONCAT 用法

    1.通过 10g 所提供的 WMSYS.WM_CONCAT 函数即可以完成 行转列的效果 select group_code, wm_concat(display_title) from DR_OPM ...

  9. bzoj1051: [HAOI2006]受欢迎的牛(tarjan强连通分量)

    强连通缩下点,出度为0有多个答案为0,否则答案为出度为0的强连通分量中点的个数. 发现一道tarjan模板题,顺便复习一波tarjan #include<iostream> #includ ...

  10. IDEA批量修改变量快捷键

    Window: Ctrl+Shift+Alt+J Mac:  Ctrl+Option+G