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. BZOJ-3670 动物园 KMP+奇怪的东西

    YveH爷再刷KMP,DCrusher看他刷KMP,跟着两个人一块刷KMP... 3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MB ...

  2. C/C++ 跨平台交叉编译、静态库/动态库编译、MinGW、Cygwin、CodeBlocks使用原理及链接参数选项

    目录 . 引言 . 交叉编译 . Cygwin简介 . 静态库编译及使用 . 动态库编译及使用 . MinGW简介 . CodeBlocks简介 0. 引言 UNIX是一个注册商标,是要满足一大堆条件 ...

  3. jsp学习(二)

    jsp运行原理 当服务器上的一个jsp页面被第一次请求标记时,服务器上的jsp引擎首先将jsp页面文件转译成一个Java文件,并编译这个java文件生成字节码文件,然后执行字节码文件响应客户的请求. ...

  4. linux 7 常见命令

    修改网卡配置文件,如下:ONBOOT=yesIPADDR=192.168.1.11NETMASK=255.255.255.0NM_CONTROLLED=no重启网卡:systemctl restart ...

  5. c++模板库(简介)

    目 录 STL 简介 ......................................................................................... ...

  6. P、NP、NPC、NP-Hard问题

    转自:http://www.matrix67.com/blog/archives/105 总结 P:能用多项式时间求解的问题NP:能用多项式时间验证的问题(但不知道能不能用多项式时间求解).存在不属于 ...

  7. php中文字符串翻转

    转自:http://www.oschina.net/code/snippet_613962_17070 <?php header("content-type:text/html;cha ...

  8. N个数全排列的非递归算法

    //N个数全排列的非递归算法 #include"stdio.h" void swap(int &a, int &b) { int temp; temp = a; a ...

  9. 你无法修改 Git 的历史记录

    转自:http://www.oschina.net/news/26241/you-can-not-change-git-history 有时候使用Git工作得小心翼翼,特别是涉及到一些高级操作,例如 ...

  10. ubuntu快速清理磁盘垃圾

    .快速清理磁盘垃圾 磁盘空间又不够用了?尝试在终端窗口中输入sudo apt-get autoremove然后输入sudo apt-get clean,前一个命令会卸载系统中所有未被使用的依赖关系,后 ...