C++ 最大子段和(动态规划)

最大子段和 对应洛谷P1115 代码见下

cpp 复制代码
#include<iostream>
using namespace std;

#define maxn 200010
#define type int
#define inf -1000000000

type getMSS(int n, type a[], type dp[]){
    //dp[i]代表以第i个数结尾的最大子段和
    type ans = inf;
    dp[0] = 0;
    for(int i=1; i<=n; ++i){
        dp[i] = a[i] + max(dp[i-1], 0);
        ans = max(dp[i], ans);
    }
    return ans;
}

type dp[maxn];
type a[maxn];

int main(){
    int n;
    cin >> n;
    for(int i=1; i<=n; ++i){
        cin >> a[i];
    }
    cout << getMSS(n, a, dp) << endl;
    return 0;
}

代码练习1 对应蓝桥云课 区间最大子序列和 代码见下

cpp 复制代码
#include<iostream>
using namespace std;

#define maxn 2010
#define type int
#define inf -1000000000

type getMSS(int n, type a[], type dp[]){
    //dp[i]代表以第i个数结尾的最大子段和
    type ans = inf;
    dp[0] = 0;
    for(int i=1; i<=n; ++i){
        dp[i] = a[i] + max(dp[i-1], 0);
        ans = max(dp[i], ans);
    }
    // 转换成前i个元素的最大子段和
    dp[0] = inf;
    for(int i=1; i<=n; ++i){
      dp[i] = max(dp[i], dp[i-1]);
    }
    return ans;
}

type dp[maxn][maxn];
type a[maxn];

int main(){
  int n;
  cin >> n;
  for(int i=1; i<=n; ++i){
    cin >> a[i];
  }
  for(int i=1; i<=n; ++i){
    getMSS(n-i+1, &a[i-1], dp[i]);
  }
  int t;
  cin >> t;
  while(t--){
    int l, r;
    cin >> l >> r;
    cout << dp[l][r-l+1] << endl;

  }

}

代码练习 3 基德的冒险之旅 蓝桥云课 代码见下

cpp 复制代码
#include<iostream>
using namespace std;

#define maxn 100010
#define type long long
#define inf -10000000000011

type getMSS(int n, type a[], type dp[]){
    //dp[i]代表以第i个数结尾的最大子段和
    type ans = inf;
    dp[0] = 0;
    for(int i=1; i<=n; ++i){
        dp[i] = a[i] + max(dp[i-1], (type)0);
        ans = max(dp[i], ans);
    }
    // 转换成前i个元素的最大子段和
    dp[0] = inf;
    for(int i=1; i<=n; ++i){
      dp[i] = max(dp[i], dp[i-1]);
    }
    return ans;
}

void swap(int n, type a[]){
  for(int i=1; i<=n/2; ++i){
    swap(a[i], a[n+1-i]);
  }
}

type dppre[maxn], dppost[maxn];
type a[maxn];

int main(){
  int n, k;
  cin >> n >> k;
  for(int i=1; i<=n; ++i){
    cin >> a[i];
  }
  getMSS(n, a, dppre);
  swap(n, a);
  getMSS(n, a, dppost);
  swap(n, dppost);
  type ans = inf;
  for(int i=1; i+k+1 <=n; ++i){
    ans = max(ans, dppre[i]+dppost[i+k+1]);
  }
  cout << ans << endl;
  return 0;
}

代码练习 4 对应蓝桥云课 可删除数组的最大子数组和 代码见下

cpp 复制代码
#include <iostream>
using namespace std;

#define maxn 100010
#define maxk 101
#define inf -1000000000
int a[maxn];
int dp[maxn][maxk];


int main()
{
  int n, k;
  cin >> n >> k;
  for(int i=1; i<=n; ++i){
    cin >> a[i];
  }
  dp[0][0] = 0;
  for(int j=1; j<=k; ++j){
    dp[0][j] = inf;
  }
  for(int i=1; i<=n; ++i){
    dp[i][0] = a[i] + max(dp[i-1][0], 0);
    for(int j=1; j<=k; ++j){
      dp[i][j] = max(dp[i-1][j] + a[i], dp[i-1][j-1]); 
    }
  }
  int ans = inf;
  for(int i=1; i<=n; ++i){
    for(int j=0; j<=k; ++j){
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << endl;
  // 请在此输入您的代码
  return 0;
}
相关推荐
写代码的【黑咖啡】5 小时前
Python 中的自然语言处理工具:spaCy
开发语言·python·自然语言处理
沐知全栈开发5 小时前
WSDL 语法详解
开发语言
蒹葭玉树5 小时前
【C++上岸】C++常见面试题目--操作系统篇(第三十期)
c++·面试·risc-v
wangmengxxw5 小时前
设计模式 -详解
开发语言·javascript·设计模式
froginwe115 小时前
ECharts 样式设置
开发语言
清风~徐~来5 小时前
【视频点播系统】AMQP-SDK 介绍及使用
开发语言
进击的小头5 小时前
设计模式落地的避坑指南(C语言版)
c语言·开发语言·设计模式
凤年徐5 小时前
容器适配器深度解析:从STL的stack、queue到优先队列的底层实现
开发语言·c++·算法
ujainu5 小时前
Flutter + OpenHarmony 游戏开发进阶:虚拟摄像机系统——平滑跟随与坐标偏移
开发语言·flutter·游戏·swift·openharmony
超绝振刀怪5 小时前
【C++ String】
c++·stl