#include<iostream>
using namespace std;
int main()
{
int Num,max=-1,first=0;
int t[10001], c[10001] = { 0 }, sum[10001] = { 0 };//sum[i]数组储存当前位置i开头的最大子串和,c[i]记录i开头最大子串长度
cin >> Num;
for (int i = 0; i < Num; i++)
cin >> t[i];
sum[Num] = -1;
c[Num] = 0;
for (int i = Num - 1; i >= 0; i--)
{
if (sum[i + 1] > 0)
{
sum[i] = t[i] + sum[i + 1];
c[i] = c[i + 1] + 1;
}
else
{
sum[i] = t[i];
c[i] = 1;
}
if (sum[i] >= max)//比较当前最大子串是否大于max
{
max = sum[i];
first = i;
}
}
if (max == -1)
{
max = 0;
cout << max << ' ' << t[first] << ' ' << t[Num+first-1] << endl;
}
else
{
cout << max << ' ' << t[first] << ' ' << t[first + c[first]-1]<<endl;
}
system("pause");
return 0;
}
PAT 1007(甲级)Maximum Subsequence Sum
最新推荐文章于 2022-05-06 09:10:14 发布