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
超时代码
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int a[],T,N,T1,j,i;
  5. scanf("%d",&T);
  6. T1=T;
  7. while(T--)
  8. {
  9. int Msum=,sum=,s=,w=;
  10. printf("case %d:\n",T1-T);
  11. scanf("%d",&N);
  12. for(i=;i<N;i++)
  13. scanf("%d",&a[i]);
  14. Msum=a[];
  15. for(j=;j<N;j++)
  16. {
  17. for(i=j;i<N;i++)
  18. {
  19. if(a[i]<=)
  20. {
  21. sum+=a[i];
  22. continue;
  23. }
  24. sum+=a[i];
  25. if(Msum<sum)
  26. {
  27. s=j;
  28. w=i;
  29. Msum=sum;
  30. }
  31. }
  32. sum=;
  33. }
  34. printf("%d %d %d\n\n",Msum,s+,w+);
  35. }
  36. return ;
  37. }
 
AC代码
  1. /*状态转移方程 d[i] = max(d[i-1]+a[i], a[i])
  2.   d[i]表示以i位置结束的最大子序列之和。*/
  3. #include<stdio.h>
  4. int main()
  5. {
  6. int a[];
  7. int T,T1;
  8. scanf("%d",&T);
  9. T1=T;
  10. while(T--)
  11. {
  12. int sum=,msum=,i,x=,y=,start=,end=,N;
  13. scanf("%d",&N);
  14. for(i=;i<N;i++)
  15. scanf("%d",&a[i]);
  16. sum=a[];
  17. msum=sum;
  18. for(i=;i<N;i++)
  19. {
  20. if(sum<)/*dp[i-1]对a[i]不仅没有贡献,反而有损害,就应该舍弃*/
  21. {
  22. x=y=i;
  23. sum=a[i];
  24. }
  25. else
  26. {
  27. sum+=a[i];
  28. y=i;
  29. }
  30. if(sum>msum)
  31. {
  32. msum=sum;
  33. start=x;
  34. end=y;
  35. }
  36. }
  37. printf("Case %d:\n",T1-T);
  38.  
  39. if(T==)
  40. printf("%d %d %d\n",msum,start+,end+);
  41. else
  42. printf("%d %d %d\n\n",msum,start+,end+);
  43.  
  44. }
  45. }

Max Sum(hd P1003)的更多相关文章

  1. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  2. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  3. Max Sum

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  4. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  5. hdu 1024 Max Sum Plus Plus

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

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

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

  7. Max Sum Plus Plus——A

    A. Max Sum Plus Plus Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To ...

  8. hdu 1003 Max sum(简单DP)

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

  9. HDU 1003 Max Sum

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

随机推荐

  1. 如何只克隆git仓库中的一个分支?

    git clone -b 例如: git clone -b 指定的分支名字

  2. Python学习笔记一,输入输出

    输出:用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下>>>print('hello,world') 也可以是多 ...

  3. 基于cygwin构建u-boot(二)gcc的C语言标准版本号(-std=)

    接上文,修改Makefile和config.mk中,.depend相关文件格式后继续编译. 四.tools文件夹的文件编译错误 错误告警如下[多行信息,请使用右侧滚动条查看]: gcc -Wall - ...

  4. Python学习(四) Python数据类型:序列(重要)

    插播一下,先了解一下Python的数据类型,Python现有的数据类型有好多,最重要的有列表.元组.字典 列表:我觉得可以对应java中的数组 list=['physics', 'chemistry' ...

  5. 我是菜鸟,我怕谁--hdu2520

    我是菜鸟,我怕谁 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. mvn打包发布

    一:打包 cmd进入工作目录运行命令 1: mvn clean 2: mvn install  3: mvn clean compile    4: mvn package -DiskipTest  ...

  7. path类和directory类对文件的路径或目录进行操作

    Path: 对文件或目录的路径进行操作(很方便)[只是对字符串的操作] 1.目录和文件操作的命名控件System.IO    2.string Path.ChangeExtension(string ...

  8. JS中各种宽度、高度、位置、距离总结

    1.window.screen 浏览器与屏幕的距离,screenX(screenLeft),screenY(screenTop) 2.window.scrollTo(x,y) 将纵向滚动条移动到相对于 ...

  9. js 属性类型

    1.访问器属性 var book = { _year: 2004, edition: 1 }; Object.defineProperty(book, "year", { get: ...

  10. gcc的stdcall扩展

    MSVC上: 如果是函数调用约定直接就写 __stdcall 之类的就行了 如果是gcc上: 函数的扩展调用约定要这样写 __attribute__((__stdcall__)),默认为__attri ...