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. 使用UIDynamicAnimator创建重力感应的View

    使用UIDynamicAnimator创建重力感应的View http://www.raywenderlich.com/zh-hans/52617/uikit-力学教程 详细教程请参考上面的链接,此处 ...

  2. 简单的python爬虫--爬取Taobao淘女郎信息

    最近在学Python的爬虫,顺便就练习了一下爬取淘宝上的淘女郎信息:手法简单,由于淘宝网站本上做了很多的防爬措施,应此效果不太好! 爬虫的入口:https://mm.taobao.com/json/r ...

  3. selenium借助send_keys实现上传(以网易邮箱为例)

    #code:utf-8from selenium import webdriverimport time#网易163邮箱dr = webdriver.Firefox()file_path = 'htt ...

  4. python UI自动化实战记录七:页面2用例编写

    使用python自带的unittest测试框架,用例继承自unittest.TestCase类. 1 引入接口类和页面类 2 setUp函数中打开页面,定义接口对象 3 tearDown函数中关闭页面 ...

  5. webpack导学

    随着前端工程越来越复杂,单独建几个文件写业务代码,这样的方式已经无法保证项目的可维护性了. 所以我们就想把不同的逻辑拆成模块,然后分开引入这些模块,每个模块自己做自己的事情,这样就可以保证项目的可维护 ...

  6. 第三章.搭建MyBatis工程环境

    1.数据库的准备: 数据库: create DATABASE mybatis: 数据表: CREATE TABLE `user` ( `id` int(10) NOT NULL AUTO_INCREM ...

  7. CVPR 2016 paper reading (3)

    DeepFashion: Powering Robust Clothes Recognition and Retrieval with Rich Annotations, Ziwei Liu, Pin ...

  8. 关于JWT.NET

    1.JWT的概念: JWT全称是Json Web Token,是一种用于双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准( RFC 7519 ),定义了一种简洁的,自 ...

  9. Java构造器(构造方法)与方法区别

    构造器,又称为构造方法.构造器用于构造该类的实例,也就是对象. 格式如下:[修饰符]  类名 (形参列表){//n条语句} 构造方法是一种特殊的方法,与一般的方法区别:  1.构造方法的名字必须与定义 ...

  10. 如何设置ASP.NET页面的运行超时时间 (转载)

    全局超时时间 服务器上如果有多个网站,希望统一设置一下超时时间,则需要设置 Machine.config 文件中的 ExecutionTimeout 属性值.Machine.config 文件位于 % ...