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. NIO三大组件简介

    NIO简介 NIO 是面向缓冲区(或者说面向块)编程的, 因为Buffer底层本质上就是内存块.数据被读取到一个缓冲区, 稍后再被它处理, 需要时数据可在缓冲区前后移动, 从而增加了处理过程中的灵活性 ...

  2. Django(九)模型:dj查询数据库的函数(方法)

    一.查询函数 通过模型类.objects属性可以调用如下函数,实现对模型类对应的数据表的查询. 函数表 函数名 功能 返回值 说明 get 返回表中满足条件的一条且只能有一条数据. 返回值是一个模型类 ...

  3. CANmonitor我自己编写的程序

    这个版本的程序, 上位机可以对电机的转速进行在线的设定,同时上位机接受电机控制器上报的母线电压,电机温度,控制器温度等. 在调试的过程中我遇见了一个问题,电机的转速的采样 . 根据协议:电机的转速为1 ...

  4. HDU 5495:LCS

    LCS  Accepts: 127  Submissions: 397  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 65536/655 ...

  5. 51nod 1065:最小正子段和

    1065 最小正子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  取消关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],从中选出一 ...

  6. 【pwnable.kr】input

    这道题是一道一遍一遍满足程序需求的题. 网上其他的题解都是用了C语言或者python语言的本地调用,我想联系一下pwntools的远程调用就写了下面的脚本, 执行效果可以通过1~4的检测,到最后soc ...

  7. 3.2Adding custom methods to mappers(在映射器中添加自定义方法)

    3.2Adding custom methods to mappers(在映射器中添加自定义方法) 有些情况下,我们需要实现一些MapStruct无法直接自动生成的复杂类型间映射.一种方式是复用其他已 ...

  8. Python MongoDB 插入文档

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  9. Ubantu学习笔记1

    重启后按e键进行编辑,在文档倒数第二行r0处修改为rw init=/bin/bash 然后F10操作,输入passwd zichua =>修改此用户名的密码,重新输入两次密码(这里密码是看不到的 ...

  10. 使用Oracle VM VirtualBox安装CentOS 7.6操作系统

    使用Oracle VM VirtualBox安装CentOS 7.6操作系统                                                               ...