HDU 1231 最大连续子序列 dp
//状态方程为c[n] = max(c[n-1]+a[n], a[n])
//c[0] = a[0]
#include <iostream>
#include <stdio.h>
#define MAX 10005
using namespace std;
int main()
{
int a[MAX];
int c[MAX];
int hold_start[MAX]; //保存起始点
int n;
while (cin >> n && n)
{
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
c[0] = a[0];
hold_start[0] = a[0];
for (int i = 1; i < n; i++)
{
if (a[i]+c[i-1] > a[i])
{
c[i] = c[i-1]+a[i];
hold_start[i] = hold_start[i-1];
}
else
{
c[i] = a[i];
hold_start[i] = a[i];
}
}
int _max = c[0];
for (int i = 1; i < n; i++)
_max = max(_max, c[i]);
if (_max < 0)
cout << "0 " << a[0] << " " << a[n-1] << endl;
else
{
for (int i = 0; i < n; i++)
if (c[i] == _max)
{
cout << _max << " " << hold_start[i] << " " << a[i] << endl;
break;
}
}
}
return 0;
}