算法笔记

1.非连续最大子段和

如果不全为负数,最大子段和所有大于等于0的元素的和;如果全为负数,最大子段和为最大的负数。

2.连续最大子段和

①无长度限制:

例题:

洛谷p1115最大子段和

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ios::sync_with_stdio(false);
    int n;
    ll ans=1ll<<63,now=0;
    cin>>n;
    while(n--)
    {
        int a;
        cin>>a;
        now+=a;
        if(now>ans)ans=now;
        if(now<0)now=0;
    }
    cout<<ans<<endl;
    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2022-03-08
猜你喜欢
  • 2021-12-11
  • 2021-08-25
  • 2022-12-23
  • 2021-12-27
  • 2021-10-23
  • 2021-12-13
相关资源
相似解决方案