| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
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;
}
原文:http://www.cnblogs.com/Aa1039510121/p/5877586.html