概述:最大子段和问题,不过同时让输出最大子段的开始和末端位置。
状态转移方程:d[i]=(d[i-1]+a[i]>a[i])?d[i-1]+a[i]:a[i];max = {d[i],1<=i<=n}
感想:基础动态规划
#include <iostream>
using namespace std;
int get(int data[] , int &l , int &r , int dl)
{
int max = -10000000 ;
l = 0 ;
r = 0 ;
int t = 1 ;
int mt = 0 ;
for(int i = 0 ; i < dl ; i++)
{
mt = mt +data[i];
if(mt > max)
{
max = mt;
l = t ; r = i+1 ;
}
if(mt < 0)
{
mt = 0 ;
t = i+2 ;
}
}
return max ;
}
int main()
{
int num ;
cin >> num ;
for(int k = 1 ; k <= num ;k++)
{
int lg = 0 ;
cin >>lg ;
int * data = new int[lg];
for(int i =0 ;i < lg ; i++)
{
cin >>data[i];
}
int l = 0 , r = lg-1 ;
int max = 0 ;
max = get(data , l , r , lg);
cout<<"Case "<<k<<":"<<endl;
cout<<max<<" "<<l<<" "<<r<<endl;
if(k!=num)
cout<<endl;
}
return 0 ;
}