Max Sum

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

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

 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for
you:  1176 1087 1069 1058 1159 
题意:求连续子序列和的最大值。
题解: 注意这道题要求是输出第一个符合条件的序列,而且存在负数,样例之间要用空行分隔。
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e5+;
  4. int a[maxn],n;
  5. int main()
  6. {
  7. int t,cas=;
  8. scanf("%d",&t);
  9. while(t--)
  10. {
  11. scanf("%d",&n);
  12. int sum=,ans=-;
  13. int s=,e=,k=;
  14. for(int i=;i<=n;i++)
  15. {
  16. scanf("%d",&a[i]);
  17. sum+=a[i];
  18. if(sum>ans)
  19. {
  20. s=k;
  21. e=i;
  22. ans=sum;
  23. }
  24. if(sum<) //0的意义就是这段数做的是负功
  25. {
  26. sum=;
  27. k=i+;
  28. }
  29. }
  30. printf("Case %d:\n",cas++);
  31. printf("%d %d %d\n",ans,s,e);
  32. if(t>) puts("");
  33. }
  34. return ;
  35. }
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e5+;
  4. int a[N],n;
  5. int main()
  6. {
  7. int t,cas=;
  8. cin>>t;
  9. while(t--)
  10. {
  11. cin>>n;
  12. int sum=,mx=-,s=,e=,ts=;
  13. for(int i=;i<=n;i++)
  14. {
  15. cin>>a[i];
  16. sum+=a[i];
  17. if(sum>mx)
  18. {
  19. mx=sum;
  20. s=ts;
  21. e=i;
  22. }
  23. if(sum<)
  24. {
  25. sum=;
  26. ts=i+;
  27. }
  28. }
  29. printf("Case %d:\n",cas++);
  30. printf("%d %d %d\n",mx,s,e);
  31. if(t) printf("\n");
  32. }
  33. return ;
  34. }
  35. /*
  36. 100
  37. 2 1 2
  38. 1 1
  39. 3 -1 1 2
  40. 2 -7 3
  41. */

牢记顺序是 加 大于 小于!!

HDU1003MAX SUM的更多相关文章

  1. HDU1003MAX SUM (动态规划求最大子序列的和)

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

  2. 动态规划:HDU1003-Max Sum(最大子序列和)

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

  3. 动态规划: HDU1003Max Sum

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

  4. C++-HDU1003-Max Sum

    时间复杂度O(n) 空间复杂度O(1) #include <cstdio> int main() { int T;scanf("%d",&T); ,n,a,l, ...

  5. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  6. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  7. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  8. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  9. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

随机推荐

  1. SpringMVC数据库链接池,以及其他相关配置

    1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  2. C++中使用array报错 requires compiler and library surpport for the ISO c++ 2011 standard

    #error This file requires compiler and library support for the \ISO C++ 2011 standard. This support ...

  3. 浅谈php对api开发的作用

    最近正在做一个手机APP的服务端API开发,虽然是基于Ruby on Rails的,做的也不太专业,不过大致相通,希望能够给你一些启发. 首先,如果是比较简单的手机APP,例如新闻客户端这样的不会涉及 ...

  4. 利用Arraylist输入学生的成绩,求出平均分和总分。

    Console.WriteLine("请输入学生人数:"); int n=int.Parse(Console.ReadLine()); ArrayList arr= new Arr ...

  5. stl 迭代器(了解)

    STL 主要是由 containers(容器),iterators(迭代器)和 algorithms(算法)的 templates(模板)构成的. 对应于它们所支持的操作,共有五种 iterators ...

  6. 使用PPA在Ubuntu上安装php5.4~5.6,7

    使用PPA在Ubuntu上安装php5.4~5.6,7 sudo apt-get install software-properties-common sudo add-apt-repository ...

  7. 使用I/O 系统调用--copy.c

    作为Linux/Unix 系统编程入门,小生按照自己可以理解的方式,改写了源copy.c源代码来自:Linux/UNIX 系统编程手册 上册 P57 #include <stdio.h>/ ...

  8. CSS transition 过渡 详解

    transition 过渡 IE10.Firefox.Chrome.Opera 支持 transition 属性. Safari 需要前缀 -webkit-. Chrome 25 以及更早版本需要前缀 ...

  9. PHP中is_numeric函数十六进制绕过0day

    0×00 简介国内一部分CMS程序里面有用到过is_numberic函数,我们先看看这个函数的结构bool is_numeric ( mixed $var )如果 var 是数字和数字字符串则返回 T ...

  10. .NET设计模式(2):单件模式(Singleton Pattern)(转载)

    概述 Singleton模 式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这就提出了一个问题:如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?客户程 序在调用某一个类时,它是不 ...