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 
题意:求连续子序列和的最大值。
题解: 注意这道题要求是输出第一个符合条件的序列,而且存在负数,样例之间要用空行分隔。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int a[maxn],n;
int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int sum=,ans=-;
int s=,e=,k=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
if(sum>ans)
{
s=k;
e=i;
ans=sum;
}
if(sum<) //0的意义就是这段数做的是负功
{
sum=;
k=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",ans,s,e);
if(t>) puts("");
}
return ;
}
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N],n;
int main()
{
int t,cas=;
cin>>t;
while(t--)
{
cin>>n;
int sum=,mx=-,s=,e=,ts=;
for(int i=;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
if(sum>mx)
{
mx=sum;
s=ts;
e=i;
}
if(sum<)
{
sum=;
ts=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",mx,s,e);
if(t) printf("\n");
}
return ;
}
/*
100
2 1 2
1 1
3 -1 1 2
2 -7 3
*/

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

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. opencv笔记2:图像ROI

    time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...

  2. BZOJ-3231 递归数列 矩阵连乘+快速幂

    题不是很难,但是啊,人很傻啊...机子也很鬼畜啊... 3231: [Sdoi2008]递归数列 Time Limit: 1 Sec Memory Limit: 256 MB Submit: 569 ...

  3. ubuntu使用ssh登入不执行.bashrc解决方法

    解决方法,可以直接输入 bash即可. 理解 bashrc 和 profile linux bashrc profile SEP 30TH, 2011 BY SUNTEYA 在一般的 linux 或者 ...

  4. HD2157How many wasy??(十大矩阵问题之八 + 邻接矩阵的应用)

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. MySQL------如何安装mysql-connector-java-5.1.38.zip

    下载地址:http://dev.mysql.com/downloads/connector/j/ 安装mysql-connector-java-5.1.38.zip:1.解压文件->把里面的my ...

  6. Fedora下安装ORACLE 11g

    一.硬件检测 1.内存检测  oracle11g要求最低1GB内存 命令: grep MemTotal  /proc/meninfo 2.交换空间检测 通常交换空间是内存1.5倍 命令: grep   ...

  7. Jquery中的 height(), innerHeight() outerHeight()区别

    jQuery中的 height innerHeight outerHeight区别 标准浏览器下: height:高度 innerHeight:高度+补白 outerHeight:高度+补白+边框,参 ...

  8. Android 中this、 getApplicationContext()、getApplication()之间的区别

    this:代表当前,在Activity当中就是代表当前的Activity,换句话说就是Activity.this在Activity当中可以缩写为this. getApplicationContext( ...

  9. [Effective JavaScript 笔记] 第12条:理解变量声明提升

    js支持词法作用域,即除了极少的例外,对变量的引用会被绑定到声明变量最近的作用域中. js不支持块级作用域,即变量定义的作用域并不是离其最近的封闭语句或代码块,而是包含它们的函数. 不了解这个会产生一 ...

  10. Centos7上使用官方YUM源安装Mysql

    1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. 安装mysql-co ...