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. 安装或删除Skype for business server组件的时候,报错"错误: 找不到 SQL 服务"

    安装或删除Skype for business server组件的时候,到了安装所有并置数据库的时候,报错“错误: 找不到 SQL 服务.确保计算机 skype.centos.com 中安装了 SQL ...

  2. CVE-2014-0322漏洞成因与利用分析

    CVE-2014-0322漏洞成因与利用分析 1. 简介 此漏洞是UAF(Use After Free)类漏洞,即引用了已经释放的内存,对指定内存处的值进行了加1.其特点在于攻击者结合flash实现了 ...

  3. 关于mysql启动问题---mysqld_safe mysqld from pid file * ended

    #在[mysqld]中添加: datadir = /usr/local/mysql/data         #添加 log-error = /usr/local/mysql/data/error.l ...

  4. 网络分析 ANP

    在许多实际问题中,各层次内部元素往往是依赖的. 低层元素对高层元素亦有支配作用,即存在反馈. 此时系统的结构更类似于网络结构.网络分析法正是适应这种需要,由AHP延伸发展得到的系统决策方法.   AN ...

  5. MS15-051 修正版Exploit(Webshell可用)

    MS15-051简介:Windows 内核模式驱动程序中的漏洞可能允许特权提升 (3057191) , 如果攻击者在本地登录并可以在内核模式下运行任意代码,最严重的漏洞可能允许特权提升. 攻击者可随后 ...

  6. IntelliJ IDEA设置编码格式

    IntelliJ IDEA设置编码格式为UTF-8

  7. Struts2注解开发

    Hibernate和spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...

  8. 【[SCOI2008]奖励关】

    又抄了一篇题解 要凉了要凉了,开学了我还什么都不会 文化课凉凉,NOIP还要面临爆零退役的历史进程 这道题挺神的,期望+状态压缩 我们设\(dp[i][S]\)表示在第\(i\)天前,捡的宝物状态为\ ...

  9. table中实现数据上移下移效果

    html 由于vue+Element项目中的table,没有开放的上移下移的api,但是能对数据操作,故思路为数组中的一条数据,再重新添加一条数据,办法有点笨,但是好歹也是实现了,望有好的办法的,请留 ...

  10. CSS:层叠样式表—position

    CSS position属性用于指定一个元素在文档中的定位方式.top,right,bottom和left属性则决定了该元素的最终位置. 常见语法 static | relative | absolu ...