Max Sum

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

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
 
 #include <stdio.h>

 int main()
{
int k,T;
scanf("%d",&T);
for(k=;k<=T;k++)
{
int i,j;
int sta,end,n,m;
int t=,max=-;
scanf("%d",&n);
sta=end=j=;
for(i=;i<=n;i++)
{
scanf("%d",&m);
t += m;
if(t>max)
{
max=t;
sta=j;
end=i;
}
if(t<)
{
t=;
j=i+;
}
}
if(k-)
printf("\n");
printf("Case %d:\n",k);
printf("%d %d %d\n",max,sta,end);
}
return ;
}

最大连续子序和

不错的资源:

http://blog.csdn.net/luxiaoxun/article/details/7438315

http://blog.csdn.net/code_pang/article/details/7772200

http://blog.csdn.net/shahdza/article/details/6302823

http://www.cnblogs.com/CCBB/archive/2009/04/25/1443455.html

参考代码:

 #include <stdio.h>
int main()
{int n,T,a,sta,end,max,k,i,p,t; scanf("%d",&T);
for(p=;p<=T;p++) {
scanf("%d",&n);
max=-; //因为一个数a 是-1000~1000的,所以这里相当于变成最小值
t=; //表示 某段连续和
sta=end=k=; // sta最大和的开始,end最大和的结束,k记录每次求和的开始
for(i=;i<=n;i++) {
scanf("%d",&a); t+=a;
if(t>max) { //记录最大连续和的值
max=t;
sta=k;
end=i;
}
if(t<) {
t=;
k=i+;
}
} if(p!=) printf("/n");
printf("Case %d:/n",p);
printf("%d %d %d/n",max,sta,end);
}
}

hdu_1003_Max Sum_201311271630的更多相关文章

  1. hdu_1003_Max Sum

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

随机推荐

  1. BZOJ DZY Loves Math系列

    ⑤(BZOJ 3560) $\Sigma_{i_1|a_1}\Sigma_{i_2|a_2}\Sigma_{i_3|a_3}\Sigma_{i_4|a_4}...\Sigma_{i_n|a_n}\ph ...

  2. 【知识总结】后缀数组(Suffix_Array)

    又是一个学了n遍还没学会的算法-- 后缀数组是一种常用的处理字符串问题的数据结构,主要由\(sa\)和\(rank\)两个数组组成.以下给出一些定义: \(str\)表示处理的字符串,长度为\(len ...

  3. websocket的原理

    首先明确几点: 1. websocket是一种协议.是html5的一种新协议: 2. 与http的区别是,它是一种双向通信协议,服务器和客户端都能主动向对方发送或接受数据: 3. websocket需 ...

  4. 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle

    问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...

  5. [转]linux下logrotate 配置和理解

    转自:http://blog.csdn.net/cjwid/article/details/1690101 对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以使用logrotat ...

  6. C#学习-程序集和反射

    准备项目 1.新建一个空的解决方案MyProj.sln 2.在该解决方案下,建一个控制台项目P01.csproj 3.在该项目下,自己新建一个类MyFirstClass.cs 查看解决方案MyProj ...

  7. Combox两级联动会经常出现的错误

    例如: 当我们遇到这种情况:(下拉框的隐藏值和显示值皆为实体类进行绑定值时)下拉框的隐藏值并不能成功获取到. 我们就可以使用下面 的方案来解决 ok ,成功获取到隐藏值. 还有一个,附加解决方案:

  8. wait、notify、notifyAll实现线程间通信

    在Java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  9. CommandBehavior.CloseConnection使用

    其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...

  10. windows server2003 多用户登陆问题解决办法

    windows server2003 多用户登陆问题解决办法 Windows Server远程登陆默认情况下只允许同时有两个用户登陆,超过两个用户会提示"超出最大连接数". 要解决 ...