http://acm.hdu.edu.cn/showproblem.php?pid=1003
问题:求里面连续最大和。
一开始的WA,发现是输出格式有错误。。。然后才发现我的测试数据在第一个为最大值时无法得到正确结果,因为我直接跳过了下标为0的数据。后来才发现。。
AC代码:
#include<iostream>
#include<cstdio>
//#include<cstring>
#include<algorithm>
using namespace std;
int dp[100010],q[100010];
int main()
{
int t,i,j,n,maxx,p;
int first,last,mx;
p = 0;
scanf("%d",&t);
while(t--)
{
p++;
//memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d",&q[i]);
}
dp[0]=q[0];
last=first=1;
maxx=q[0]; //这里一开始我忘了。。直接跳过了第一个
for(i = 1; i < n; i++)
{
dp[i] = max(dp[i-1]+q[i],q[i]);
if(dp[i] > maxx)
{
maxx = dp[i];
last = i+1;
}
}
mx = 0;
for(i = last-1; i >= 0; i--)
{
mx = q[i]+mx;
if(mx == maxx)
{
first = i+1;
}
}
printf("Case %d:\n",p);
printf("%d %d %d\n",maxx,first,last);
if(t != 0)
{
printf("\n");
}
}
return 0;
}