Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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 题意不说 和hdu1231很相似 刚刚写了1231,但被这题坑了 maxsum我开始初值设了0 一直wa
后面测试数据才发现自己错了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
typedef long long ll;
#define N 100005
using namespace std;
int a[N];
int b[N];
ll dp[N];
int main()
{
int t;
int n;
int i,j;
int first,next;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
ll maxsum=-99999999;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp));
first=1;
for(i=1;i<=n;i++)
{
if(dp[i-1]+a[i]>=a[i])
dp[i]=dp[i-1]+a[i];
else
{
dp[i]=a[i];
first=i;
}
b[i]=first;
maxsum=max(maxsum,dp[i]);
}
for(i=1;i<=n;i++)
{
if(maxsum==dp[i])
{
next=i;
//break;
}
}
printf("Case %d:\n",j);
if(j!=t)
printf("%I64d %d %d\n\n",maxsum,b[next],next);
else
printf("%I64d %d %d\n",maxsum,b[next],next);
}
return 0;
}

hdu 1003的更多相关文章

  1. dp 动态规划 hdu 1003 1087

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

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

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

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

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

  4. HDU 1003(A - 最大子段和)

    HDU   1003(A - 最大子段和) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A 题目: ...

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

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

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

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

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

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

  8. HDU 1003 动态规划

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...

  9. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

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

  10. HDU 1003 最大连续子段和

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)M ...

随机推荐

  1. PHP发送邮件的两种方式

    1.用SMTP方式发送邮件.引入phpmailer文件包,然后在文件中 <?php require_once("phpmailer/class.phpmailer.php") ...

  2. ORA-15028: ASM file '..' not dropped; currently being accessed --转载

    Couple of weeks ago we had a problem with one of our busiest databases. The FRA was filling quite ra ...

  3. [PAT]数列求和(20)

    #include "stdio.h" #include "malloc.h" #include "math.h" void calc(int ...

  4. java 单例设计模式

    1.饿汉单例设计模式:  步骤 :   1.定义一个私有的静态成员变量来引用对象(私有的静态对象),设置对象唯一.   2.私有化构造方法,防止new对象.   3.创建一个公开的静态方法,返回上面的 ...

  5. ssh整合(http://blog.csdn.net/songanling/article/details/22454973)

    http://blog.csdn.net/songanling/article/details/22454973

  6. Linux 账户信息显示和实现账户安全

    一.账户信息显示 1.groups命令 使用groups命令可以显示指定用户账户的组群成员身份. [root@redhat2 ~]# groups --help Usage: groups [OPTI ...

  7. 通过案例对 spark streaming 透彻理解三板斧之一: spark streaming 另类实验

    本期内容 : spark streaming另类在线实验 瞬间理解spark streaming本质 一.  我们最开始将从Spark Streaming入手 为何从Spark Streaming切入 ...

  8. hive中的桶

    hive中有桶的概念,对于每一个表或者分区,可以进一步组织成桶,说白点,就是更细粒度的数据范围.hive采用列值哈希,然后除以桶的个数以求余的方式决定该条记录存放在哪个桶当中.使用桶的好处:1.获得更 ...

  9. git常用操作

    批量删除以bran开头的本地分支 git branch |grep 'bran'|xargs git branch -d

  10. Delphi编译的程序,查看控件名称方法

    使用SpyLite24这个软件可以查看程序所使用的控件名称