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. linux下挂载CDROM命令

    cdrom装载在/dev/cdrom 下面. 挂载时在权限范围内建立一个文件夹.例如mkdir /home/dang/cdtmp 然后执行 mount dev/cdrom /home/dang/cdt ...

  2. php 程序员的历程

    今天一朋友该找工作了. 问了我好多 我整理了下  希望有些帮助 以下内容纯属个人感觉如果有不恰当的地方请忽略.... 我做的是项目的包工 就是把销售拿下的项目整合后给我们实现功能. --------- ...

  3. shell之rm -rf的别名设置

    vim ~/.bashrc alias rm='read -p "Are you ready?" y && [ $y == "y" ] & ...

  4. Smallest Difference(POJ 2718)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6740   Accepted: 18 ...

  5. The kth great number(优先队列)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  6. Rectangle and Square

    Description Little Petya very much likes rectangles and especially squares. Recently he has received ...

  7. Resharper使用

    Resharper进阶一:简要介绍 官方下载地址下载地址  注册码: 用户名: User 注册码:D9d09DSYJel9IyuDU4btAQwZcbLugUad 面对这样一个问题:为什么.net能够 ...

  8. Java 遍历Map

    Set<Map.Entry<String, String>> aSet = map.entrySet(); Iterator<Map.Entry<String, S ...

  9. Hadoop: the definitive guide 第三版 拾遗 第十二章 之Hive分区表、桶

    Hive分区表 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念.分区表指的是在创建表时指 ...

  10. 数据可视化的优秀入门书籍有哪些,D3.js 学习资源汇总

    习·D3.js 学习资源汇总 除了D3.js自身以外,许多可视化工具包都是基于D3开发的,所以对D3的学习就显得很重要了,当然如果已经有了Javascript的经验,学起来也会不费力些. Github ...