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
超时代码
 #include<stdio.h>
int main()
{
int a[],T,N,T1,j,i;
scanf("%d",&T);
T1=T;
while(T--)
{
int Msum=,sum=,s=,w=;
printf("case %d:\n",T1-T);
scanf("%d",&N);
for(i=;i<N;i++)
scanf("%d",&a[i]);
Msum=a[];
for(j=;j<N;j++)
{
for(i=j;i<N;i++)
{
if(a[i]<=)
{
sum+=a[i];
continue;
}
sum+=a[i];
if(Msum<sum)
{
s=j;
w=i;
Msum=sum;
}
}
sum=;
}
printf("%d %d %d\n\n",Msum,s+,w+);
}
return ;
}
 
AC代码
 /*状态转移方程 d[i] = max(d[i-1]+a[i], a[i])
  d[i]表示以i位置结束的最大子序列之和。*/
#include<stdio.h>
int main()
{
int a[];
int T,T1;
scanf("%d",&T);
T1=T;
while(T--)
{
int sum=,msum=,i,x=,y=,start=,end=,N;
scanf("%d",&N);
for(i=;i<N;i++)
scanf("%d",&a[i]);
sum=a[];
msum=sum;
for(i=;i<N;i++)
{
if(sum<)/*dp[i-1]对a[i]不仅没有贡献,反而有损害,就应该舍弃*/
{
x=y=i;
sum=a[i];
}
else
{
sum+=a[i];
y=i;
}
if(sum>msum)
{
msum=sum;
start=x;
end=y;
}
}
printf("Case %d:\n",T1-T); if(T==)
printf("%d %d %d\n",msum,start+,end+);
else
printf("%d %d %d\n\n",msum,start+,end+); }
}

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. js 判断js函数、变量是否存在 JS保存和删除cookie操作,判断cookie是否存在的方法

    //是否存在指定函数 function isExitsFunction(funcName) {    try {        if (typeof(eval(funcName)) == " ...

  2. python 连接操作数据库(二)

    一.我们接着上期的博客继续对ORM框架进行补充,顺便把paramiko模块也给大家讲解一下: 1.ORM框架: 在连接操作数据库的第一个博客中也已经说了,sqlalchemy是一个ORM框架,总结就是 ...

  3. 从几个sample来学习JAVA堆、方法区、JAVA栈和本地方法栈

    最近在看<深入理解Java虚拟机>,书中给了几个例子,比较好的说明了几种OOM(OutOfMemory)产生的过程,大部分的程序员在写程序时不会太关注Java运行时数据区域的结构: 感觉有 ...

  4. Nginx限制模块研究

    已有方案:无非两种 1. 限制连接个数: 是指一台机多线程或多进程开启请求. 解决方案-ngx_http_limit_conn_module 缺点: 定义$variable为单机时,只能限制已有ngi ...

  5. ListView.setOnItemClickListener无效

    如果ListView中的单个Item的view中存在checkbox,button等view,会导致ListView.setOnItemClickListener无效, 事件会被子View捕获到,Li ...

  6. TCP Keepalive HOWTO

    TCP Keepalive HOWTO Fabio Busatto <fabio.busatto@sikurezza.org> 2007-05-04 Revision History Re ...

  7. 场景2 nginx 错误日志格式:

    nginx 错误日志格式: 2016/09/01 11:23:36 [error] 28388#0: *14549 open() "/var/www/zjzc-web-frontEnd/im ...

  8. 使用docker打造spark集群

    前提条件:安装好了docker,见我的另一篇博客,Docker安装 有两种方式, Spark官方repo里,docker文件夹下的脚本.官方的这个脚本封装很薄,尽可能把必要的信息展示出来. AMPLa ...

  9. Bash Shell 快捷键的学习使用

    原文地址: http://dbanotes.net/tech-memo/shell_shortcut.html 这篇 Bash Shell Shortcuts 的快捷键总结的非常好.值得学习.下面内容 ...

  10. Remove Duplicates from Sorted List II 解答

    Question Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dist ...