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
----------------------------------------------------------------------------------------------------------------
 
/*
设a[i]为和最大序列的起点,则如果a[i]是负的,那么它不可能代表最优序列的起点,因为任何包含a[i]作为起点的子序列都可以通过a[i+1]作起点而得到改进。
类似的,任何负的子序列也不可能是最优子序列的前缀。
注意:如果全为负值,则取序列最大值。
*/
 
#include <stdio.h>
#define SIZE 100100 int main() {
//数据个数,数组长度。
int n, m;
//序列
int a[SIZE];
int i, j, k;
long long sum;
long long max;
int leftBorder, rightBorder, tempBorder; //freopen("F:\\input.txt","r",stdin);
scanf("%d", &n);
for (i = 0; i < n; i++)
{
sum = 0;
max = -1001;
memset(a, 0, sizeof(a));
leftBorder = rightBorder = tempBorder = 0;
scanf("%d", &m);
for (j = 0; j < m; j++)
{
scanf("%d", &a[j]);
sum += a[j];
//如果当前和大于目前的最大和,则将sum设为最大和,并且更新边界
if (sum > max)
{
max = sum;
leftBorder = tempBorder;
rightBorder = j;
}
//如果当前和小于0,则当前序列不可能为最优序列的起点,重置和与边界
if (sum < 0)
{
sum = 0;
tempBorder = j + 1;
} }
printf("Case %d:\n", i + 1);
printf("%lld %d %d\n",max, leftBorder + 1, rightBorder + 1);
if (i != (n - 1))
printf("\n");
}
//freopen("con", "r", stdin);
//system("pause");
return 0;
}

  

 
 

ACM1003:Max Sum的更多相关文章

  1. HDU1244:Max Sum Plus Plus Plus

    题目链接:Max Sum Plus Plus Plus 题意:在n个数中取m段数使得这m段数之和最大,段与段之间不能重叠 分析:见代码 //dp[i][j]表示前i个数取了j段的最大值 //状态转移: ...

  2. HDU 1024:Max Sum Plus Plus(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

  3. HDU3415:Max Sum of Max-K-sub-sequence(单调队列)

    Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left ...

  4. HDU 1003:Max Sum(DP,连续子段和)

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

  5. HDU 1024:Max Sum Plus Plus(DP,最大m子段和)

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

  6. HDU-1003:Max Sum(优化)

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

  7. HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和

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

  8. HDU 1003:Max Sum

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

  9. LeetCode 363:Max Sum of Rectangle No Larger Than K

    题目链接 链接:https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/description/ 题解&代码 1 ...

随机推荐

  1. Asp.net让某一页设置成gb2312或utf-8的方法

    有些需求,一定要用到utf-8格式,在web.config里面设置<globalization             requestEncoding="utf-8"     ...

  2. [使用经验]cocostudio UI编辑器的裁剪

    日志-2015/03/16 描述:在程序使用UI编辑器导出文件的时候,该panel中大于panel的部分都没有显示出来,例如人物,一些特效等 原因:UI编辑器panel都勾上了裁剪 解决:在编辑器中把 ...

  3. Hyperledger Fabric 1.0 学习搭建 (五)--- 启动Fabric多节点集群

    5.1.启动orderer节点服务 上述操作完成后,此时各节点的compose配置文件及证书验证目录都已经准备完成,可以开始尝试启动多机Fabric集群. 首先启动orderer节点,切换至order ...

  4. sql2005 和 mysql 定时备份批处理

    保存为sqlbak.bat  目录我直接放winrar 的根目录了 或者拷贝一个winrar.exe 具体目录随意. 然后添加计划任务个人的话建议一周或者一天 虚拟主机等 建议每周或者每月 @echo ...

  5. maven添加oracle依赖失败

    由于Oracle授权问题,Maven3不提供oracle JDBC driver 可以到maven中央仓库去下载依赖,网址:http://repo.spring.io/plugins-release/ ...

  6. 使用C#检验.NET FrameWork版本

    代码如下: public static bool checkFrameWork(string destVersion) { bool ver1 = GetVersionFromRegistry(des ...

  7. commons-lang(一)

    写在开头的话: 在工作中,经常会要用到一些公用的组件包,比如日期,String这些等,换过几次工作的朋友对这事肯定比较深刻,如果自己有积累的还好,对于没有积累的同学,尤其是规模较小的公司,怕是又要重头 ...

  8. luogu3368树状数组模板2

    题目链接:https://www.luogu.org/problemnew/show/P3368 题意:与模板1不同的是这题的操作是树状数组并不在行的区间更新和单点查找,如果按照模板1那样写肯定会T. ...

  9. JDBC(5)ResSetMetaData&DatabaseMetaData&获取数据库主键的值

    ResSetMetaData 可用于获取关于 ResultSet 对象中列的类型和属性信息的对象: getColumnName(int column):获取指定列的名称 getColumnCount( ...

  10. 压缩Windows系统磁盘

    compact /compactOS:always #压缩所有 OS 二进制文件并将系统状态设置为"压缩" compact /compactOS:never #减压缩所有 OS 二 ...