HDU1003

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
    求最大子序列的和,及其左右开始下标。
   甚是惭愧,现在是10月份,此题我在3月份已经在HDU上交过并AC了,但是没写题解,导致现在题出来又不会,现在必须补上。
   此前我想的是,遇到a[i]<0时,重新更新sum值,但是后来一想不对,比如这个 :  2  -1   100  更新sum的话,最大只能加到100而不是101.
   初始用dp保存输入的每个数字,而后的过程中,dp[i]为以 i 位置为结尾的最大值。如果走的过程中,dp[i-1]<0了,即累加出现<0的情况,那么就没必要再加了,因为再加就会减小。所以要从dp[i]重新累加,记录到此时的左端点  k=i;    每次for更新一次最大值(dp[i]>max),l=k,r=i;
    

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+;
int dp[maxn]; //maxx=max{dp[i-1]+a[i],a[i]}
int main()
{
int t;
cin>>t;
int mid=t;
int ac=;
while(t--)
{
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>dp[i];
int maxx=dp[];
int l=,r=;
int k=;
for(int i=;i<=n;i++)
{
// cout<<dp[i-1]<<endl;
if(dp[i-]>=)
{
dp[i]+=dp[i-];
}
else
{
k=i;
}
if(dp[i]>maxx)
{
maxx=dp[i];
l=k;
r=i;
}
}
printf("Case %d:\n",ac);
printf("%d %d %d\n",maxx,l,r);
if(ac!=mid)
cout<<endl;
ac++;
}
return ;
}

    注意输出,最后一句。每个样例之间输出一个空行,但是注意结尾不能有空行!

最大连续子序列和,以及开始、结束下标(HDU 1003)的更多相关文章

  1. DP专题训练之HDU 1231 最大连续子序列

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

  2. HDU 1231:最大连续子序列 解题报告

    第一次写博客, 自己总结写出了一道题感觉值得保存. 自己总结的规律:求最大连续子序列, 可以先求包括第N项在内的前N项最大值, (因为每一项都求过后, 前N项最大值最大的那一个元素所连续的序列即为最大 ...

  3. [ACM_其他] 总和不小于S的连续子序列的长度的最小值——尺缩法

    Description: 给定长度为n的整数数列,A[0],A[1],A[2]….A[n-1]以及整数S,求出总和不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. Input: 输入数据有 ...

  4. ACM-DP之最大连续子序列——hdu1231

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  5. HDU 1231——最大连续子序列(DP)

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  6. HDU 1231 最大连续子序列 (动态规划)

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. 最大连续子序列(DP)

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

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

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

  9. HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

随机推荐

  1. NIFI

    Apache nifi 第一篇(概述) Apache nifi 第二篇(小白初试) nifi数据对接流程初次尝试 NIFI ExecuteSQL配置教程(1.8) Processor(处理器)之配置 ...

  2. 浅谈arduino的bootloader

    在arduino的板子上,作为核心的avr单片机往往都会烧录一个bootloader,这个叫做bootloader的东东其实是arduino研发团队针对arduino板子开发的一小段代码,借助于这段代 ...

  3. nginx proxy_pass解释

    在当前大部分对外提供的web服务会使用nginx做负载均衡,日常相关的proxy_pass设置有: 以http://192.168.1.101/proxy/test.html进行访问为例子 第一种: ...

  4. ExcelPackage导入导出,命名空间一定要是EPPlus

    1.引入EPPlus.dll,旧版的是OfficeOpenXml.dll,最好使用EPPlus2.调用 string path = UploadExecl(batchUpload.BinaryExce ...

  5. 小程序地图开发周边信息POI展示为列表

    ##首先附上效果图 在我前面的文章中我详述过如何使用百度地图API来开发小程序的地图,所以这里面就不说基础内容了. 直说如下: ##1.如何获取列表: //分类存储 makertap: functio ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-camera

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  7. (java) webdriver 启动firefox driver时,加载firebug的扩展

    去网上下载一个firebug.xpi(对应版本, 我的ff是17,可以使用firebug-1.11.4.xpi,最好使用非firefox浏览器下载,不然提示你直接安装到firefox) @Before ...

  8. OpenPose 开源库安装

    一.下载openpose下载openpose 使用Git直接下载openpose git clone https://github.com/CMU-Perceptual-Computing-Lab/o ...

  9. python类、super函数

    #PYTHON语言及其应用学习笔记 1.创建简单的类 class Person(): #python中特殊的对象初始化方法__init__,一个特殊的函数名 #当你在类声明里定义__init__()方 ...

  10. cf 609E.Minimum spanning tree for each edge

    最小生成树,lca(树链剖分(太难搞,不会写)) 问存在这条边的最小生成树,2种情况.1.这条边在原始最小生成树上.2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去).( ...