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. VMware安装VMware tool后mount /dev/cdrom /mnt成功挂载含rpm包的镜像

    安装虚拟机后如果不安装vmware tool使用mount /dev/cdrom /mnt可以成功挂在含rpm包的镜像,但是安装VMware tool后挂在后/mnt中是错误的内容.查了半天后无果,自 ...

  2. ejb3persistence.jar javax.persistence的注解配置

    JPA注解持久化类很方便,需要jar包:ejb3-persistence.jar.我用以下三个类来说明用法.  sh原创 转载请注明: http://67566894.iteye.com/blog/6 ...

  3. JavaScript的DOM_操作表格

    一.使用HTML标签创建表格 thead.tfoot.caption标签在一个表格中只能有一个    tbody.tr.td.th标签在一个表格中可以有N个 <table border=&quo ...

  4. websphere 配置库中已存在应用程序,异常处理

    from:http://mengdboy.iteye.com/blog/1677379 出现此问题的原因之一:操作界面上没有卸载完成. 进行一下操作: 1.删除 $WAS_HOME/profiles/ ...

  5. websphere8 从安装到部署 测试集群应用程序 安装j2ee程序(非常详细)

    目录1. 准备安装文件2. 安装Installation Manager3. 为Installation Manager指定安装资源库4. 创建部署管理器概要文件5. 创建定制概要文件并联合到部署管理 ...

  6. 在一个应用中如果同一个Spring 的IOC容器被实例化两次就会出现 CannotAcquireResourceException 异常

    现象描述:我在一个Junit 的测试类中实例化IOC容器 : ac = new ClassPathXmlApplicationContext("applicationContext.xml& ...

  7. Java 学习笔记1

    最近开始学习Java. <%@ page language="java" import="java.util.*" pageEncoding=" ...

  8. mac zsh 快捷定位文件

    brew install zsh vim ~/.zshrc plugins=(git autojump zsh-autosuggestions zsh-syntax-highlighting yarn ...

  9. Linux Shell 编程 文件转置问题

    给定一个文件 file.txt,转置它的内容. 你可以假设每行列数相同,并且每个字段由 ' ' 分隔. 示例: 假设 file.txt 文件内容如下: name age alice 21 ryan 3 ...

  10. es6之proxy和reflect

    一.proxy //Proxy和Reflect //供应商 let obj={ time:"2017-11-21", name:"net", _r:123 } ...