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

时间:2021-02-07 04:39:08

测试样例之间输出空行,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 ;
}