简单的DP, 求最大连续子段和, 只不过需要加一个记录路径的……
#include <iostream>
using namespace std;
struct Node
{
int sum;
int s;
int e;
}dp[100001];
int main( )
{
int t, n, a[100001];
int i, j, num;
cin>>t;
for(i=1; i<=t; i++)
{
cin>>n>>a[0];
dp[0].sum=a[0];
dp[0].s=0;
dp[0].e=0;
for(j=1; j<n; j++)
{
cin>>a[j];
if( dp[j-1].sum<0 ) //这里不取等号, 因为 If there are more than one result, output the first one
{
dp[j].sum=a[j];
dp[j].s=j;
dp[j].e=j;
}
else
{
dp[j].sum=dp[j-1].sum+a[j];
dp[j].s=dp[j-1].s;
dp[j].e=j;
}
}
num=0;
for(j=1; j<n; j++)
{
if(dp[j].sum>dp[num].sum)
num=j;
}
cout<<"Case "<<i<<":"<<endl;
cout<<dp[num].sum<<" "<<dp[num].s+1<<" "<<dp[num].e+1<<endl;
if( i!=t ) cout<<endl;
}
return 0;
}