文章
25
粉丝
364
获赞
8
访问
219.0k
/*
* Description: 最大序列和 (http://noobdream.com/DreamJudge/Issue/page/1172/)
* Author: 鱼翔浅底
* Date: 2021-02-18 16:54:58
*/
#include <cstdio>
#include <cstdlib>
using namespace std;
//计算最大序列和
long long MaxSequenceSum(long long S[], int N)
{
long long ans = S[0], tmp = 0;
for (int i = 0; i < N; i++)
{
tmp+=S[i];
if (ans<tmp)//更新最大子列和
{
ans=tmp;
}
if (tmp<0)//子列和为负,则重新开始累加
{
tmp=0;
ans=(ans<S[i])?S[i]:ans;//更新最大子列和
}
}
return ans;
}
int main()
{
int N;
long long S[100000];//用int部分数据溢出
while (~scanf("%d", &N))
{
for (int i = 0; i < N; i++)
{
scanf("%lld", S + i);
}
printf("%lld\n", MaxSequenceSum(S, N));
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发