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
 
题目主要是求连续最大子序列的和,并输出最大子序列的左边界和右边界。
注意输出的格式为每两组数据之间输出一个空行,最后一组数据没有。
 
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
int t,n;
int i,j,k=;
int Max,sum,x,y,l,d;
scanf("%d",&t);
while(t--)
{
Max=sum=-INF;
scanf("%d",&n);
for(i=; i<=n; i++)
{
scanf("%d",&d);
if(sum+d<d)
sum=d,l=i;
else
sum+=d;
if(Max<sum)
{
x=l;
y=i;
Max=sum;
}
}
if(k)
printf("\n");
printf("Case %d:\n",++k);
printf("%d %d %d\n",Max,x,y);
}
return ;
}

HDU 1003 MAXSUM(最大子序列和)的更多相关文章

  1. HDU 1003 maxsum

    #include<iostream> #include<vector> using namespace std; typedef struct { int maxsum; in ...

  2. 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)

    问题描述:       连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...

  3. hdu 1003 hdu 1231 最大连续子序列【dp】

    HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...

  4. HDU 1231 最大连续子序列 --- 入门DP

    HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...

  5. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  6. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  7. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  8. dp 动态规划 hdu 1003 1087

    动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...

  9. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

随机推荐

  1. cgi fast-cgi php-fpm区别

    php-cli 是php在系统执行的程序,直接执行php文件: cgi和fast-cgi的区别1.cgi和fast-cgi都是php解析协议,负责解析服务器分发过来的php动态文件:cgi程序就会去解 ...

  2. php libev扩展使用

    在WorkerMan源码分析 - 实现最简单的原型文章中提到了libevent网络库,前者和libev都是事件驱动编程库高性能.简单说libev对libevent做了改进和精简.libevent使用全 ...

  3. InertialNav

    https://github.com/priseborough/InertialNav Instructions To Run Simulink Model Note : Simulink model ...

  4. pthreads v3下一些坑和需要注意的地方

    一.子线程无法访问父线程的全局变量,但父线程可以访问子线程的变量 <?php class Task extends Thread { public $data; public function ...

  5. YII2中查询生成器Query()的使用

    YII2中的yii\db\Query给我们提供了非常丰富的方法,方便我们构建复杂的SQL语句. Query()与createCommand最大区别在于,后者直接使用我们写好的SQL语句,前者通过参数和 ...

  6. javascript 生存周期

    生存周期: 局部 JavaScript 变量 在 JavaScript 函数内部声明的变量(使用 var)是局部变量,所以只能在函数内部访问它.(该变量的作用域是局部的). 您可以在不同的函数中使用名 ...

  7. js函数定义和调用

    由于JavaScript的函数也是一个对象,上述定义的abs()函数实际上是一个函数对象,而函数名abs可以视为指向该函数的变量. var abs = function (x) { if (x > ...

  8. Hyperscan与Snort的集成方案

    概况 Hyperscan作为一款高性能的正则表达式匹配库,非常适用于部署在诸如DPI/IPS/IDS/NGFW等网络解决方案中.Snort (https://www.snort.org) 是目前应用最 ...

  9. [转载]linux中sed的用法

    转自:http://www.cnblogs.com/emanlee/archive/2013/09/07/3307642.html sed命令行格式为:         sed [-nefri]  ‘ ...

  10. Python数据库工具类MySQLdb使用

    MySQLdb模块用于连接mysql数据库. 基本操作 # -*- coding: utf-8 -*-       #mysqldb       import time, MySQLdb       ...