C++区间DP

区间DP代码框架见下 对应蓝桥云课 石子合并 代码见下

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

#define type int
#define maxn 210
//dp[i][j]就代表了区间[i,j]的元素最优值
type dp[maxn][maxn];
int a[maxn], sum[maxn];
int n;

type IntervalDP_Opt(type a, type b){
  return min(a, b);
}

type IntervalDP_ValueInf(){
  return 1000000000;
}

type IntervalDP_ValueInit(){
  return 0;
}

type IntervalDP_CalcState(int l, int r){
  type ans = IntervalDP_ValueInf();
  for(int k = l; k < r; ++k){
    type v = dp[l][k] + dp[k+1][r] + (sum[r] - sum[l-1]);
    ans = IntervalDP_Opt(ans, v);
  }
  return ans;
}

type IntervalDP_Solve(int maxlen, int maxr){
  type ans = IntervalDP_ValueInf();
  // 1 枚举区间长度
  for(int i=1; i <= maxlen; ++i){
    // 2 枚举区间起点
    for(int j=1; j+i-1 <= maxr; ++j){
      int l = j;
      int r = j + i - 1;
      if(i == 1){
        dp[l][r] = IntervalDP_ValueInit();
      }else{
        dp[l][r] = IntervalDP_CalcState(l, r);
      }
      if(i == maxlen){
      ans = IntervalDP_Opt(ans, dp[l][r]);
      }
    }

  }
  return ans;
}

int main()
{
  cin >> n;
  for(int i=1; i <= n; ++i){
    cin >> a[i];
    sum[i] = sum[i-1] + a[i];
  }
  cout << IntervalDP_Solve(n, n) << endl;
  // 请在此输入您的代码
  return 0;
}

代码练习 1,对应蓝桥云课 小蓝吃苹果 代码见下

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

#define type int
#define maxn 510
//dp[i][j]就代表了区间[i,j]的元素最优值
type dp[maxn][maxn];
int a[maxn], sum[maxn];
int n;

type IntervalDP_Opt(type a, type b) {
    return min(a, b);
}

type IntervalDP_ValueInf() {
    return 1000000000;
}

type IntervalDP_ValueInit() {
    return 1;
}

type IntervalDP_CalcState(int l, int r) {
    type ans = IntervalDP_ValueInf();
    for (int k = l; k < r; ++k) {
        ans = IntervalDP_Opt(ans, dp[l][k] + dp[k + 1][r]);
    }
    if (a[l] == a[r]) {
        ans = IntervalDP_Opt(ans, dp[l + 1][r - 1] + (l + 1 == r));
    }
    return ans;
}

type IntervalDP_Solve(int maxlen, int maxr) {
    type ans = IntervalDP_ValueInf();
    // 1 枚举区间长度
    for (int i = 1; i <= maxlen; ++i) {
        // 2 枚举区间起点
        for (int j = 1; j + i - 1 <= maxr; ++j) {
            int l = j;
            int r = j + i - 1;
            if (i == 1) {
                dp[l][r] = IntervalDP_ValueInit();
            }
            else {
                dp[l][r] = IntervalDP_CalcState(l, r);
            }
            if (i == maxlen) {
                ans = IntervalDP_Opt(ans, dp[l][r]);
            }
        }

    }
    return ans;
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << IntervalDP_Solve(n, n) << endl;
    // 请在此输入您的代码
    return 0;
}

代码练习2 对应蓝桥云课 课上小游戏 代码见下

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

#define type int
#define maxn 210
//dp[i][j]就代表了区间[i,j]的元素最优值
type dp[maxn][maxn];
int a[maxn], mul[maxn][maxn];
int n;

type IntervalDP_Opt(type a, type b){
  return max(a, b);
}

type IntervalDP_ValueInf(){
  return -1000000000;
}

type IntervalDP_ValueInit(){
  return 0;
}

type IntervalDP_CalcState(int l, int r){
  type ans = IntervalDP_ValueInf();
  for(int k=l; k<r; ++k){
    type a = mul[l][k];
    type b = mul[k+1][r];
    type v = dp[l][k] + dp[k+1][r] + a*b/10;
    ans = IntervalDP_Opt(ans, v);
  }
  return ans;
}

type IntervalDP_Solve(int maxlen, int maxr){
  type ans = IntervalDP_ValueInf();
  // 1 枚举区间长度
  for(int i=1; i <= maxlen; ++i){
    // 2 枚举区间起点
    for(int j=1; j+i-1 <= maxr; ++j){
      int l = j;
      int r = j + i - 1;
      if(i == 1){
        dp[l][r] = IntervalDP_ValueInit();
      }else{
        dp[l][r] = IntervalDP_CalcState(l, r);
      }
      if(i == maxlen){
      ans = IntervalDP_Opt(ans, dp[l][r]);
      }
    }

  }
  return ans;
}

int main()
{
  cin >> n;
  int n2 = n*2;
  for(int i=1; i<=n; ++i){
    cin >> a[i];
    a[n+i] = a[i];
  }
  for(int i=1; i<=n2; ++i){
    mul[i][i-1] = 1;
    for(int j=i; j<=n2; ++j){
      mul[i][j] = mul[i][j-1] * a[j] % 10;
    }
  }
  cout << IntervalDP_Solve(n, n2) << endl;
  // 请在此输入您的代码
  return 0;
}

代码练习3 对应蓝桥云课 涂色 代码见下

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

#define type int
#define maxn 55
//dp[i][j]就代表了区间[i,j]的元素最优值
type dp[maxn][maxn];
char a[maxn];
int n;

type IntervalDP_Opt(type a, type b){
  return min(a, b);
}

type IntervalDP_ValueInf(){
  return 1000000000;
}
//当区间长度为1的时候,该状态下的DP值
type IntervalDP_ValueInit(){
  return 1;
}

type IntervalDP_CalcState(int l, int r){
  type ans = IntervalDP_ValueInf();
  for(int k = l; k < r; ++k){
    ans = IntervalDP_Opt(ans, dp[l][k]+dp[k+1][r]);
  }
  if(a[l] == a[r]){
    ans = IntervalDP_Opt(ans, dp[l+1][r]);
    ans = IntervalDP_Opt(ans, dp[l][r-1]);
  }
  return ans;
}

type IntervalDP_Solve(int maxlen, int maxr){
  type ans = IntervalDP_ValueInf();
  // 1 枚举区间长度
  for(int i=1; i <= maxlen; ++i){
    // 2 枚举区间起点
    for(int j=1; j+i-1 <= maxr; ++j){
      int l = j;
      int r = j + i - 1;
      if(i == 1){
        dp[l][r] = IntervalDP_ValueInit();
      }else{
        dp[l][r] = IntervalDP_CalcState(l, r);
      }
      if(i == maxlen){
      ans = IntervalDP_Opt(ans, dp[l][r]);
      }
    }

  }
  return ans;
}

int main()
{
  cin >> &(a[1]);
  n = strlen(&a[1]);
  cout << IntervalDP_Solve(n, n) << endl;
  // 请在此输入您的代码
  return 0;
}
相关推荐
罗西的思考17 分钟前
【轻量化复刻大模型 / KIMI K3】K3 PostTrain 后训练交互式演示
人工智能·算法·机器学习
Navigator_Z2 小时前
LeetCode //C - 1318. Minimum Flips to Make a OR b Equal to c
c语言·算法·leetcode
JosieBook3 小时前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
电科_银尘3 小时前
【大模型算法与工程 39】PPO:策略比率、裁剪目标与训练稳定性
人工智能·算法·机器学习
shehuiyuelaiyuehao3 小时前
算法68,字符串相乘
算法
Huangjin007_4 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南
c++·算法
敲上瘾5 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
驭渊的小故事6 小时前
LeetCode 33:搜索旋转排序数组——从二分查找到旋转数组的区间判定
算法·leetcode·职场和发展
炸膛坦客6 小时前
单片机/C/C++八股:(三十三)取别名
c语言·c++·单片机
小飞在努力7 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端