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];
【代码】  
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. using namespace std;
  6. const int maxn = ;
  7. int n, a[maxn];
  8. int d[maxn];
  9. void dp()
  10. {
  11. memset(d, , sizeof(d));
  12. d[] = a[];
  13. for(int i = ; i < n; i++)
  14. {
  15. if(d[i-] > ) d[i] = d[i-]+a[i];
  16. else d[i] = a[i];
  17. }
  18. //cout << endl;
  19. int st = , en = ;
  20. int max_ = d[];
  21. for(int i = ; i < n; i++)
  22. {
  23. if(d[i]>max_)
  24. {
  25. max_ = d[i];
  26. en = i;
  27. }
  28. }
  29. st = en;
  30. for(int j = en-; j>=; j--)
  31. {
  32. if(d[j] < ) break;
  33. else st = j;
  34. }
  35. printf("%d %d %d\n", max_, st+, en+);
  36. }
  37.  
  38. int main()
  39. {
  40. int T; scanf("%d", &T);
  41. for(int kase = ; kase < T; kase++)
  42. {
  43. scanf("%d", &n);
  44. for(int i = ; i < n; i++)
  45. scanf("%d", &a[i]);
  46. if(kase) printf("\n");
  47. printf("Case %d:\n", kase+);
  48. dp();
  49.  
  50. }
  51. return ;
  52. }
  

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. 被mysql中的wait_timeout坑了

    今天被mysql里的wait_timeout坑了         网上能搜到很多关于mysql中的wait_timeout相关的文章,但是大多数只是说明了他的作用,而且都说这个参数要配合那个inter ...

  2. Python if..else

    200 ? "200px" : this.width)!important;} --> 一.介绍 1.完整形式 if <条件判断1>: <执行1> e ...

  3. js字符串常用判断方法

    转自:http://blog.sina.com.cn/s/blog_6819fa800100j5t6.html 一.方法介绍 function obj$(id)                     ...

  4. MongoDB 快速入门--初级

    数据库的操作一般来说都是CRUD,这其中最难的就是查询,所有所我们先来了解MongoDB中的 插入(insert) 说到插入,我们就必须得说说如何创建数据库,如何创建集合,然后才是如何创建文档. 在这 ...

  5. Oracle DataGuard 物理Standby 搭建(上)

    物理standby database 环境搭建 Arch asysnc Oracle Dataguard host IP Oracle_sid DB_unique_name FAL_server FA ...

  6. hdu 5279 Reflect phi 欧拉函数

    Reflect Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest_chi ...

  7. Android - FrameLayout覆盖顺序

    FrameLayout覆盖顺序 本文地址: http://blog.csdn.net/caroline_wendy FrameLayout: Child views are drawn in a st ...

  8. delphi CoolBar这个怎么弄没了

    CoolBar这个怎么弄没了 像Windows这样的     procedure TForm1.Button1Click(Sender: TObject); begin    CoolBar1.Fix ...

  9. 偶然发现关于网页JavaScript脚本无法正常运行的原因

    客户常常打电话投诉公司的销售系统有问题, 后来发现有的客户直接把网址设为受限网站,才导致系统无法正常执行.改动后正常.

  10. AS3 Signals

    在项目中,使用as3内置事件框架必须通过自定义事件才可以实现值的传递,大量自定义事件.定义常量和整个事件派发的管理.添加侦听器.移除侦听器,或多或少都会带来大量的代码,而signals这个框架思想原来 ...