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
难道这种水题也要PE好几次???
case中间有空行 T之间没空行!!怪不得AC率那么低
这么一小破题还需要输出那么多中间变量查错→_→
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100006
long long q[N],dp[N],tmp,maxn;
int t,n;
int pos,pos1;
int main()
{
//freopen("cin.txt","r",stdin);
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
printf("Case %d:\n",cas);
scanf("%d",&n);
maxn=-0x3f3f3f3f;
//memset(dp,-0x3f3f3f3f,sizeof(dp));
for(int i=1;i<=n;i++)scanf("%lld",&q[i]);
// for(int i=1;i<=n;i++)printf("%lld ",q[i]);
dp[0]=0;
for(int i=1;i<=n;i++)
{
dp[i]=q[i]>(dp[i-1]+q[i])?q[i]:(dp[i-1]+q[i]);
if(maxn<dp[i])
{
maxn=dp[i];
pos=i;
//printf("pos=%d maxn=%d\n",pos,maxn);
}
}
// printf("pos=%d maxn=%d\n",pos,maxn);
tmp=maxn;
// printf("tmp=%lld\n",tmp);
for(int k=pos;k>=1;k--)
{
tmp=tmp-q[k];
// printf("k=%d q[k]=%d tmp=%lld \n",k,q[k],tmp);
if(tmp==0) {pos1=k;}
}
printf("%lld %d %d\n",maxn,pos1,pos);
if(cas!=t)printf("\n");
}
return 0;
}
上面是自己丑陋的代码→_→ 而且没有领悟到dp的精髓 自以为是的认为自己会了 而且 这么写 跟下一篇 hdu1024根本靠不上关系(⊙﹏⊙)b看看邝斌的
#include<stdio.h>
#define MAXN 100000
int main()
{
int T,iCase,i,sum,maxsum,j,x,y,a;
int n;
scanf("%d",&T);
iCase=0;
while(T--)
{
iCase++;
sum=0;
maxsum=-20000;
i=1;
scanf("%d",&n);
for(j=1;j<=n;j++)
{
scanf("%d",&a);
sum+=a;
if(maxsum<sum)
{
maxsum=sum;
x=i;
y=j;
}
if(sum<0)
{
i=j+1;
sum=0;
}
}
printf("Case %d:\n",iCase);
printf("%d %d %d\n",maxsum,x,y);
if(T>0)printf("\n");
}
return 0;
}