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. flask 常用数据模型模板

    1.一对多关系模型 示例代码 class Role(db.Model): """角色表""" __tablename__ = 'roles' ...

  2. POJ 2796:Feel Good 单调栈经典题

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11626   Accepted: 3212 Case T ...

  3. Arduino 的读串口与写串口

    //准备一下             while(Serial.available()>0)        WifiSerial.write(Serial.read());         wh ...

  4. Linux下的文件目录树结构

    Linux下的文件目录及文件结构 一.文件和文件夹 在Linux系统下,一切皆是文件.就连Linux本身也是基于文件表示的操作系统. 1.文件:文件在Linux系统之下,一般分为两种:一是一般性文件, ...

  5. 百度easydl之图像分类构建是否佩戴口罩模型

    2020-02-14 今天试了下百度esaydl的图像分类方面的功能,其优点是主需要上传自己的数据集,不需要关注模型训练,就可以得到相应的结果.最后得到的模型可以调用云api在本地进行运行. 网址:h ...

  6. 114-PHP判断类变量是否相同

    <?php class ren{ //定义人类 } class mao{ //定义人类 } $ren=new ren(); //实例化人类的对象 $ren_a=new ren(); //实例化人 ...

  7. kibana下载与安装

    目录 简介 下载 安装 测试 简介 Kibana是一个为ElasticSearch 提供的数据分析的 Web 接口.可使用它对日志进行高效的搜索.可视化.分析等各种操作.安装之前有话说: 安装路径不要 ...

  8. pytorch随笔

    pytorch中transform函数 一般用Compose把多个步骤整合到一起: 比如说 transforms.Compose([ transforms.CenterCrop(10), transf ...

  9. P 1015 德才论

    转跳点:

  10. Day3-T1

    原题目 Describe:暴力出奇迹 [ 注意步长0.5!] code: #include<bits/stdc++.h> using namespace std; inline int r ...