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. Laravel Configuration

    Introduction All of the configuration files for the Laravel framework are stored in the app/config d ...

  2. TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView

    先上一张图: 这是使用UITextView时用到的iOS7新增加的类:NSTextContainer.NSLayoutManager.NSTextStorage及其相互关系: 这三个新出的类还没有在官 ...

  3. 算法之旅,直奔<algorithm>之十七 find_first_of

    find_first_of(vs2010) 引言 这是我学习总结 <algorithm>的第十七篇,find_first_of是匹配的一个函数.<algorithm>是c++的 ...

  4. 常见MFC UI界面库

    Xtrme toolkit,BCGControlBar,SkinMagic,AppFace,Skin++,Uskin++,SYGUI,LibUIDK,GuiToolkit,GardenUI等等,除了后 ...

  5. ADO.NET 快速入门(三):从存储过程获取输出参数

    一些存储过程通过参数返回值.当参数在SQL表达式或者存储过程中被定义为“输出”,参数值会返回给调用者.返回值存储在 OleDbCommand 或者 SqlCommand 对象的参数集合的参数里.   ...

  6. Unix-Linux编程实践 学习点滴

    1.结构体最后定义一个char p[0]有什么作用 2. 3. 4. 1.结构体最后定义一个char p[0] 有什么作用 这是个广泛使用的常见技巧,常用来构成缓冲区.比起指针,用空数组有这样的优势: ...

  7. C# 添加类库依赖

  8. Codeforces Gym 100733A Shitália 计算几何

    ShitáliaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...

  9. 批量导出表数据到CSV文件

    需求:把oracle数据库中符合条件的N多表,导出成csv文本文件,并以表名.csv为文件名存放. 实现:通过存储过程中UTL_FILE函数来实现.导出的csv文件放入提前创建好的directory中 ...

  10. [Angular 2] Understanding OpaqueToken

    When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...