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
 
【题意】 给n个数,求最大连续元素的和;并输出起点和终点。
【分析】
  设d[i]为以第i个元素为终点的最大连续元素和。则
  状态转移方程:d[i] = d[i-1] > 0 ? d[i-1]+a[i] : a[i];
 
  思路应该比较好理解,如果d[i-1]<0, 那么无论a[i]为何值,其和总不如单独的a[i]大;反之如果d[i-1]>0, 那么无论a[i]为何值,其和总大于单独的a[i];
【代码】  
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn = ;
int n, a[maxn];
int d[maxn];
void dp()
{
memset(d, , sizeof(d));
d[] = a[];
for(int i = ; i < n; i++)
{
if(d[i-] > ) d[i] = d[i-]+a[i];
else d[i] = a[i];
}
//cout << endl;
int st = , en = ;
int max_ = d[];
for(int i = ; i < n; i++)
{
if(d[i]>max_)
{
max_ = d[i];
en = i;
}
}
st = en;
for(int j = en-; j>=; j--)
{
if(d[j] < ) break;
else st = j;
}
printf("%d %d %d\n", max_, st+, en+);
} int main()
{
int T; scanf("%d", &T);
for(int kase = ; kase < T; kase++)
{
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%d", &a[i]);
if(kase) printf("\n");
printf("Case %d:\n", kase+);
dp(); }
return ;
}
  

HDU 1003 - Max Sum(难度:*)的更多相关文章

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

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

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

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

  3. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

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

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

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

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

  6. HDU 1003 Max Sum (动规)

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

  7. hdu 1003 Max sum(简单DP)

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

  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 解题报告

    题目大意:求一串数字中,几个连续数字加起来最大值,并确定起始和最末的位置. 思路:这是一题DP题,但是可以用尺取法来做.我一开始不会,也是看了某大神的代码,然后有人告诉我这是尺取法,现在会了. //尺 ...

随机推荐

  1. HDU 5821 Ball (贪心)

    Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

  2. Linux下MongoDB备份脚本

    #!/bin/bash today=`date +%Y%m%d` mongodump -h localhost -d salary -o /home/chzhao/mongobackup/$today ...

  3. AutoCAD.NET二次开发:创建自定义菜单(COM)

    当我们要在CAD中创建自定菜单时,可以引用COM组件来实现. 下面是实现方式: 1.新建类库项目,并引用CAD目录(我这里用的是CAD2008)下的acdbmgd.dll.acmgd.dll,并将引用 ...

  4. IE中的文档兼容性

    文档兼容性可定义 Internet Explorer 呈现网页的方式, 具体可以参考 https://msdn.microsoft.com/zh-cn/library/cc288325(v=vs.85 ...

  5. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

  6. IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书

    一. 申请证书1. 到受信任的机构申请 略 2. 到自建的证书服务器申请 a. 安装证书服务 通过控制面板中的“添加/删除程序”,选择“添加/删除Windows组件”.在Windows组件向导中找到“ ...

  7. [置顶] a+=1/a=+1/a-=1区别-c语言

    1.解释 a+=1/a=+1/a-=1 含义 a+=1 实质等于 a += 1,也就是等于 a = a + 1: a=+1 实质等于 a = +1:[因为运算符中没有=+,很多人误以为是 a =+ 1 ...

  8. kaptcha验证码插件的使用

    kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.K ...

  9. 【M16】谨记80-20法则

    1.开始编写代码时,不要过多考虑效率,而应该首先考虑逻辑的清晰性和代码的可读性. 2.后期通过测试找到效率的瓶颈所在,而不是靠猜测.然后,针对性地去解决.也就是80%的时间去解决这20%的代码.

  10. C# 启动外部程序的几种方法

    . 启动外部程序,不等待其退出. . 启动外部程序,等待其退出. . 启动外部程序,无限等待其退出. . 启动外部程序,通过事件监视其退出. // using System.Diagnostics; ...