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. 认识元数据和IL(上) <第三篇>

    说在,开篇之前 很早就有说说Metadata(元数据)和IL(中间语言)的想法了,一直在这篇开始才算脚踏实地的对这两个阶级兄弟投去些细关怀,虽然来得没有<第一回:恩怨情仇:is和as>那么 ...

  2. 【Xamarin挖墙脚系列:Xamarin 上台讲述PPT呵呵呵】

    http://pan.baidu.com/s/1kUrwQft

  3. 在COM组件中调用JavaScript函数

    转载自: http://blog.csdn.net/cheungmine/article/details/1451489 要求是很简单的,即有COM组件A在IE中运行,使用JavaScript(JS) ...

  4. atoi 和itoa用法

    1.itoa 在linux下没有itoa这个函数 原型:char  *itoa(int   value,char   *string,int   radix)                   用法 ...

  5. UESTC_酱神赏花 2015 UESTC Training for Dynamic Programming<Problem C>

    C - 酱神赏花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Submi ...

  6. hdu 1502 Regular Words_高精度+dp

    题意:问按规则排成的串有多少个A(c)>= B(c) >= C(c) 思路:因为写大整数太累,就偷懒了一下直接用java水过 import java.math.BigInteger; im ...

  7. tomcat配置访问日志,访问首页主目录

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" ...

  8. 【MSP是什么】MSP认证之项目管理与项目群管理的区别

    通常所说的项目管理是指运用各种相关知识.技能.方法与工具,为满足或超越项目有关各方对项目的要求与期望,所开展的各种计划.组织.领导.控制等方面的活动.具体包括项目范围管理.项目时间管理.项目成本管理. ...

  9. ajax+json+java

    1.首先下载json所以依赖的包, Json-lib 最新版 json-lib-2.3-jdk15.jar,其官方网站是:http://json-lib.sourceforge.net/可以直接dow ...

  10. 如何把UIView转成UIImage,解决模糊失真问题

    最近工作中,遇到一个需求,需要把一个UIView对象转成UIImage对象显示.经过网络搜索,找到如下答案: ? 1 2 3 4 5 6 7 8 -(UIImage*)convertViewToIma ...