Note here that the sub-segment is continuous, and the sub-sequence is not continuous.
(1) Violent solution
//最大子段和 暴力 直接三层循环 暴力所有区间 然后找最大的子段和
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
int a[2000];
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n;
cin >> n;
for(int i = 0;i < n; i++)
cin>>a[i];
int ans =0;
for(int i = 0;i < n;i++){
for(int j =i ; j<n ;j++){
int sum = 0;
for(int k = i;k<=j;k++){
sum +=a[k];
}
ans=max(sum,ans);
}
}
cout<< ans <<endl;
return 0;
}
//最大子段和 暴力优化
//a(i~j)可分解为 a(j)+a(i~j-1) 然后发现可以将刚刚的最里层循环给省掉 从而节省一部分时间
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
int a[2000];
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin>>a[i];
int ans =0;
for(int i = 0; i < n; i++)
{
int sum = 0;
for(int j =i ; j<n ; j++)
{
sum +=a[j];
ans=max(sum,ans);
}
}
cout<< ans <<endl;
return 0;
}
(2) The idea of using divide and conquer algorithm
a[0:n-1] can be divided into a[0:n/2] and a[n/2+1:n-1], the two ends of a[1:n] are the largest sub-segments and there are the following three types Happening
1. The maximum sub-segment sum of a[0:n-1] is the same as the maximum sub-segment sum of a[0:n/2], which can be solved by recursion
2. The maximum sub-segment sum of a[0:n-1] is the same as the maximum sub-segment sum of a[n/2+1:n-1], which can be solved by recursion
3. The sum of the largest sub-segments of a[0:n-1] is sum(ak) k from i to j 1<=i<=n/2,n/2+1<=j<=n Obviously a[ n/2] and a[n/2+1] are in the optimal subsequence. So you can calculate sl=max (sum(ak)) k from i to n/2 in a[0:n/2]. 0<=i<=n/2 Similarly a[n/2+1:n ] Calculate sr=max (sum(ak)) k from n/2+1 to nn/2+1<=i<=n-1
//最大子段和 分治写法
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
int a[2000];
int f(int l,int r){
int sum = 0;
if(l==r)
sum=max(a[l],0);
else{
int mid = (floor)((l+r)/2);
int lsum = f(l,mid);
int rsum = f(mid+1,r);
int sl= 0;
int tmp = 0;
for(int i = mid; i >= l; i--){
tmp += a[i];
sl=max(sl,tmp);
}
tmp = 0;
int sr = 0;
for(int i=mid + 1;i <= r; i++){
tmp += a[i];
sr=max(sr,tmp);
}
sum=max(sl+sr,max(lsum,rsum));
}
return sum;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n;
cin >> n;
for(int i = 0;i < n; i++)
cin>>a[i];
int ans = f(0,n-1);
cout<< ans <<endl;
return 0;
}
(3) dp solution
Take the set of examples and run the dynamic transfer equation directly, and then find the maximum each time
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
#define ll long long
const int N=1e5+6;
ll dp[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
memset(dp,0,sizeof(dp));
ll maxans = -1;
int a;
for(int i=0;i<n;i++){
cin>>a;
dp[i]=max(dp[i],dp[i-1]+a);
maxans=max(maxans,dp[i]);
}
cout << maxans << endl;
return 0;
}