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. Codeforces 723d [暴力dfs]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给n,m和k,n和m为所给矩阵的高和宽.k是要求最多剩下的湖的数量. 在所给的矩阵中,*代表陆地,.代表水. 湖的定义是一片连续的水(上下左右四 ...

  2. 黑马程序员_ C语言基础(一)

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ 开发过程: 编写->编译(只编译源文件,编译成*.o  只会检测语法是否合理,不会检测函数是 ...

  3. 13. Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Y ...

  4. php源码之计算两个文件的相对路径

    <?php //计算出两个文件的相对路径即path2相对于$path1的相对路径 // http://www.manongjc.com/article/1342.html function ge ...

  5. 注册页面JS前台校验

    运行效果图: HTML代码: <script> function checkForm(){ //校验用户名 //获得用户名文本框的值 var username=document.getEl ...

  6. Mono addin 学习笔记 5 TypeExtensionPoint

    1. Attribute声明方式 定义扩展点: [TypeExtensionPoint]public interface ICommand{        void Run();} 定义扩展: [Ex ...

  7. RedHat5--yun源无法使用问题解决

    YUM是Redhat Linux在线安装更新及软件的工具,但是这是RHEL5的收费功能,如果没有购买Redhat的服务时不能使用RHEL5的更新源的,会提示注册. 由于CentOS是从Redhat演化 ...

  8. IP地址更改小工具(bat命令)

    为了方便切换IP地址,特编制bat命令代码来实现,将以下代码复制到txt文本中,然后保存为bat文件,双击bat文件运行即可. 通过bat命令运行,自动修改IP地址,代码如下: @echo off c ...

  9. MySQL 批量插入 Update时Replace

    建一张试验表如下: 一.批量插入 MySQL的INSERT有一种写法如下: INSERT INTO person VALUES (NULL,'关羽', '2016-04-22 10:00:00'), ...

  10. 百度地图API示例之添加/删除工具条、比例尺控件

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...