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. URL编码表、Base64编码表、HTTP消息含义

    URL编码表 backspace 8% A 41% a 61% § %A7 Õ %D5 tab 9% B 42% b 62% « %AB Ö %D6 linefeed %0A C 43% c 63% ...

  2. linux-ubuntu 安装配置Redis

    >wget http://download.redis.io/releases/redis-3.2.6.tar.gz #下载redis源码包 >tar -zxvf redis-3.2.6. ...

  3. BadgeView 圆形数字提醒 购物车常用

    实际上BadgeView这个类就是继承TextView的.很多TextView中设置字体的方法都适用于BadgeView. 1. setTargetView(View) --> 设置哪个控件显示 ...

  4. CMake命令

    CMake手册详解,作者翻译的很详细,以下是自己进行的摘录: CMake80个命令(详细解释可以看here) CMD#1: add_custom_command为生成的构建系统添加一条自定义的构建规则 ...

  5. apache开启gzip压缩

    1.在httpd.conf中去掉下面的#号 LoadModule headers_module modules/mod_headers.so LoadModule deflate_module mod ...

  6. Python 内置函数 memoryview

    转载自:https://www.cnblogs.com/sesshoumaru/p/6035548.html 英文文档: class memoryview(obj) memoryview object ...

  7. 使用DirectX作渲染过程

    1. 首先知道渲染代码放置位置.渲染代码放在WinMain消息循环中 while(msg.message!=WM_QUIT) { if(PeekMessage(****) { TranslateMes ...

  8. JVM 类加载器 (二)

    1.类加载器(ClassLoader)负责加载class文件,class文件在文件开头有特定的文件标识,并且ClassLoader只负责 class 文件的加载,至于class文件是否能够运行则由Ex ...

  9. ES6 中 let and const

    let 和 const 命令 let 命令 基本用法 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; v ...

  10. AUC和ROC

    https://www.cnblogs.com/gatherstars/p/6084696.html