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. 【Leetcode】【Medium】Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  2. UIButton中的**EdgeInsets是做什么用的?

    UIButton中的**EdgeInsets是做什么用的? UIEdgeInsetsMake Creates an edge inset for a button or view.An inset i ...

  3. console和windows子系统

    https://blog.csdn.net/ilvu999/article/details/8050292

  4. 自动备份软件 —— Syncovery 7.98s Pro/Enterprise

    SynCovery自动备份软件原名Super Flexible Synchronizer,是目前功能最为强大的实时自动备份工具,连FTP.WebDAV等全部支持!最近从V6开始改用比较好记.易懂的新名 ...

  5. Java---页面之间传值跳转

    从首页A进入页面B,然后从B页面登录,成功后跳转到A页面,并打印一句话“登录成功”,传值需要用的后台的. 在B页面写: <%     session.setAttribute("key ...

  6. 汇编试验十五:安装新的int 9中断例程

    安装新的int 9中断例程(按'A'键后显示满屏幕的'A') int 9 是外中断,同样,程序编写还是和其他中断例程类似,安装(复制),调用: 不同点是在于,他要从端口读取数据60h, Source ...

  7. 【[HNOI2006]超级英雄】

    看到楼下有大佬说了网络流做法,来给大佬配个代码 我们只有我可能都觉得如果不动态加边的话\(dinic\)可能跑不了这种需要中途退出的二分图匹配 正当我准备去敲匈牙利的时候突然想到这个题可以二分啊 于是 ...

  8. 最简单的PS渐变导入方法 photoshop渐变插件素材导入教程

    photoshop渐变插件素材可以让用户更好更直接,更快速地设计出自己想要的效果作品.网上有多种多样的ps渐变,那么Mac版Ps渐变怎么导入呢?这里我来和大家分享一下photoshop渐变插件素材导入 ...

  9. 【转】XZip and XUnzip - Add zip and/or unzip to your app with no extra .lib or .dll

    原文:http://www.codeproject.com/Articles/4135/XZip-and-XUnzip-Add-zip-and-or-unzip-to-your-app-w Downl ...

  10. 我的第一个C++程序

    准备抽空学习C++了,不知道自己以后能不能坚持下去,去百度查了一下入门,大多数朋友都是选择用VC++或者VS,而我这里用的是C-Free 5 ,安装包也只有十几兆. 用起来也方便.对于初学者而言够用了 ...