测试样例之间输出空行,if(t>0) cout<<endl;

这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行。

dp[i]表示以a[i]结尾的最大值,则:dp[i]=max(dp[i]+a[i],a[i])

解释:

以a[i]结尾的最大值,要么是以a[i-1]为结尾的最大值+a[i],要么是a[i]自己本身,就是说,要么是连同之前的

构成一个多项的字串,要么自己单独作为一个字串,不会有其他的可能了。

状态规划的对状态的要求是:当前状态只与之前的状态有关,而且不影响下一个状态。

Max Sum

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

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
#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn = ;
int a[maxn];
int dp[maxn];
int main()
{
int t;
scanf("%d",&t);
int cas=;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",a+i);
dp[]=a[];
int ans = dp[];
int end_pos=;
for(int i=;i<n;i++)
{
dp[i]=max(dp[i-]+a[i],a[i]);
if(ans<dp[i])
{
ans=dp[i];
end_pos=i;
}
}
int tmp=;
int sta_pos;
for(int i= end_pos;i>-;i--)
{
tmp+=a[i];
if(tmp==ans)
{
sta_pos=i;
}
}
printf("Case %d:\n%d %d %d\n",cas++,ans,sta_pos+,end_pos+);
if(t>) cout<<endl;
}
return ;
}

hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行的更多相关文章

  1. hdu 1003 Max sum(简单DP)

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

  2. hdu 1003 Max Sum(基础dp)

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

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

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

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

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

  5. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  6. hdu 1003 Max Sum (DP)

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

  7. 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 ...

  8. HDU 1003 Max Sum(DP)

    点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...

  9. hdu 1024 Max Sum Plus Plus DP

    Max Sum Plus Plus Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

随机推荐

  1. winrt 页面进入动画

    private async void DoAnimistion(){Storyboard storyboard = new Storyboard(); using (IEnumerator<De ...

  2. Divide and conquer:K Best(POJ 3111)

     挑选最美的珠宝 题目大意:挑选k个珠宝使得∑a/∑b最大,输出组合数 最大化平均值的标准题型,二分法就好了,一定要注意范围(10e-7),如果是10e-8就会tle,10e-6就是wa #inclu ...

  3. 面向对象的小demo

    两个类如下 package cao.com.duixiang; import java.util.Arrays; public class OtherTest { //求max public int ...

  4. IOS关于录音,播放实现总结

    //音频录制(标准过程5,9更新) 准备:导入AVFoundation框架及头文件 1 设置会话类型,允许播放及录音AVAudioSession *audioSession = [AVAudioSes ...

  5. mybatis配置问题

    //当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始. <bean id="sqlSession" class= ...

  6. vector 去重复

    ①首先将vector排序 sort( vecSrc.begin(), vecSrc.end() ); // 1,2,3,3,4,4,6,7,8,9 ②然后使用unique算法,unique返回值是重复 ...

  7. mysql 查看用户的权限

    show grants for 'username'@'%';

  8. AIX 配置网卡

    ifconfig en0 10.1.1.100 netmask 255.255.255.0 alias

  9. 聊聊SOA面向服务架构

    什么是SOA SOA(Service-Oriented Architecture),即面向服务的架构.SOA是一种粗粒度.松耦合服务架构,服务之间通过简单.精确定义接口进行通讯,不涉及底层编程接口和通 ...

  10. java中常用的工具类(三)

    继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类       ...