AcWing 3652. 最大连续子序列
作者:
闪回
,
2024-03-18 11:57:16
,
所有人可见
,
阅读 8
最大子段和不难求,但是需要记录一下方案,多开一个数组
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
int n;
int w[N];
int f[N],g[N];
int main()
{
while(cin>>n)
{
memset(w,0,sizeof w);
memset(f,0,sizeof f);
memset(g,0,sizeof g);
for (int i = 0; i < n; i ++ )cin>>w[i];
for(int i = n-1;i>=0;i--)
{
if(w[i] >= f[i+1] + w[i])
{
f[i] = w[i];
g[i] = i;
}
else
{
f[i] = f[i+1] + w[i];
g[i] = g[i+1];
}
}
int res = -1,l = 0,r = 0;
for (int i = 0; i < n; i ++ )
{
if(f[i] > res)
{
res = f[i];
l = i;
r = g[i];
}
}
if(res < 0)cout<<0<<" "<<0<<" "<<0<<endl;
else cout<<res<<" "<<l<<" "<<r<<endl;
}
return 0;
}