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. HTML5 canvas 在线画笔绘图工具(二)

    Canvas+Javascript 带图标的工具条制作 TToolbar 工具条是由一个TToolbar对象和两个按钮对象(TImageButton.TColorButton)组成,因为之前我大部分时 ...

  2. [C++程序设计]字符数组的赋值与引用

    只能对字符数组的元素赋值,而不能用赋值语句对整个数组赋值. char c[5]; c={′C′,′h′,′i′,′n′,′a′}; //错误,不能对整个数组一次赋值 c[0]=′C′; c[1]=′h ...

  3. php过滤iphone的emoji表情

    public static function removeEmoji($text) { $clean_text = ""; // Match Emoticons $regexEmo ...

  4. PXE简要配置过程

    目录 1.所需服务 2.简要配置过程     1.dhcp服务     2.tftp服务     3.提供pxelinux.0配置文件     4.提供系统所需文件 1.所需服务:     dhcp服 ...

  5. c++基础五个题(二)

    一.c++中static的作用 1.隐藏:当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性,static函数作用就是为了隐藏 2.可以保持变量的持久:存储在静态数据区的变 ...

  6. linux mysql 优化

    第一 在 /etc/my.cnf 中加入 skip-name-resolve ,重启mysql,这样就能禁用DNS解析,连接速度会快很多.不过,这样的话就不能在MySQL的授权表中使用主机名了而只能用 ...

  7. linux环境下deb格式文件转换成rpm格式

    以 alien_8.87.tar.gz 为例: 下载.安装 alien_8.87.tar.gz [root@shyn ~]# wget http://ftp.de.debian.org/debian/ ...

  8. T-SQL 脚本

    1.USE语句 USE语句用于设置当前数据库,如果没有USE语句,那么就由执行脚本的任何用户来确定执行脚本时当前数据库是正确的.如果只是一个通用脚本,那么省去USE语句实际上可能更有益.通常,如果在脚 ...

  9. SQL Server索引设计 <第五篇>

    SQL Server索引的设计主要考虑因素如下: 检查WHERE条件和连接条件列: 使用窄索引: 检查列的选择性: 检查列的数据类型: 考虑列顺序: 考虑索引类型(聚集索引OR非聚集索引): 一.检查 ...

  10. Fatal error: Allowed memory size of 8388608 bytes exhausted

    这两天安装bugfree,更换了一个数据量较大的库,结果打开bug详情页要么是空白页,要么就报如题的错误,错误信息还包括C:\wamp\www\bugfree\Include\Class\ADOLit ...