当前位置: 首页 > backend >正文

【每日一题 | 2025年5.19 ~ 5.25】动态规划相关题

在这里插入图片描述

个人主页:Guiat
归属专栏:每日一题

在这里插入图片描述

文章目录

  • 1. 【5.19】P1130 红牌
  • 2. 【5.20】P1387 最大正方形
  • 3. 【5.21】P1507 NASA的食物计划
  • 4. 【5.22】P1616 疯狂的采药
  • 5.【5.23】P1679 神奇的四次方数
  • 6. 【5.24】P1049 [NOIP 2001 普及组] 装箱问题
  • 7. 【5.25】P1044 [NOIP 2003 普及组] 栈

正文

1. 【5.19】P1130 红牌

题目链接:https://www.luogu.com.cn/problem/P1130

【分析】
二维动态规划问题,从倒数第2步考虑,取两种方案中最小的一种,然后一直做到第一步,找最小值。

【AC_Code】

#include <iostream>
#include <algorithm>
#include <climits>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;const int N = 2e3 + 10; int a[N][N], ans = INT_MAX;void solve()
{int n, m; cin >> n >> m;for (int i = 0; i < m; i ++) for (int j = 0; j < n; j ++) cin >> a[i][j];for (int j = n - 2; j >= 0; j --) for (int i = 0; i < m; i ++)a[i][j] += min(a[i][j + 1], a[(i + 1) % m][j + 1]);for (int i = 0; i < m; i ++) ans = min(a[i][0], ans);cout << ans << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

2. 【5.20】P1387 最大正方形

题目链接:https://www.luogu.com.cn/problem/P1387

【分析】

简单动态规划题,状态转移方程:f[i][j] = min(f[i - 1][j - 1], min(f[i - 1][j], f[i][j - 1])) + 1。

  • f[i][j]代表正方形右下角坐标为i,j所构成的最大正方形边长。
  • 注意下标从1开始比较方便,从0开始会导致非法访问,需考虑边界情况。

【AC_Code】

#include <iostream>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;int a[105][105], f[105][105], ans;void solve()
{int n, m; cin >> n >> m;for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++){cin >> a[i][j];if (a[i][j] == 1) f[i][j] = min(f[i - 1][j - 1], min(f[i - 1][j], f[i][j - 1])) + 1;ans = max(f[i][j], ans);}cout << ans << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

3. 【5.21】P1507 NASA的食物计划

题目链接:https://www.luogu.com.cn/problem/P1507

【分析】
01背包问题。

【AC_Code】

#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;const int N = 55; int H[N], T[N], K[N], f[501][501];void solve()
{int h, t, n; cin >> h >> t >> n;for (int l = 0; l < n; l ++) cin >> H[l] >> T[l] >> K[l];for (int l = 0; l < n; l ++)for (int i = h; i >= H[l]; i --) for (int j = t; j >= T[l]; j --){f[i][j] = max(f[i][j], f[i - H[l]][j - T[l]] + K[l]);}cout << f[h][t] << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

4. 【5.22】P1616 疯狂的采药

题目链接:https://www.luogu.com.cn/problem/P1616

【分析】
完全背包问题。

【AC_Code】

#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;
using ll = long long;const int N = 1e4 + 10, M = 1e7 + 10; ll a[N], b[N], f[M];void solve()
{int t, m; cin >> t >> m;for (int i = 1; i <= m; i ++){cin >> a[i] >> b[i];for (int j = a[i]; j <= t; j ++)f[j] = max(f[j], f[j - a[i]] + b[i]);}cout << f[t] << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

5.【5.23】P1679 神奇的四次方数

题目链接:https://www.luogu.com.cn/problem/P1679

【分析】
可以dfs+剪枝,也可以dp。

【AC_Code1 | 动态规划】

#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;const int N = 1e5 + 10; int a[N], f[N];void solve()
{int m; cin >> m; for (int i = 1; i <= m; i ++) f[i] = INT_MAX;for (int i = 1; i <= sqrt(sqrt(m)); i ++) a[i] = pow(i, 4);for (int i = 1; i <= sqrt(sqrt(m)); i ++) for (int j= a[i]; j <= m;j ++){f[j] = min(f[j], f[j - a[i]] + 1);}cout << f[m] <<'\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

【AC_Code2 | 搜索】

#include <iostream>
#include <cmath>
#include <climits>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;int n, ans = INT_MAX;void dfs(int sum, int cnt, int last)
{if (sum > n || cnt > ans) return ;if (sum == n) { ans = cnt; return ; }for (int i = sqrt(sqrt(n)) + 1; i >= last; i --) dfs(sum + i * i * i * i, cnt + 1, i);
}void solve() { cin >> n; dfs(0, 0, 1); cout << ans << '\n'; }int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

6. 【5.24】P1049 [NOIP 2001 普及组] 装箱问题

题目链接:https://www.luogu.com.cn/problem/P1049

【分析】
01背包。

【AC_Code】

#include <iostream>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;const int N = 40, M = 2e4 + 10; int v[N], f[M];void solve()
{int V, n; cin >> V >> n;for (int i = 1; i <= n; i ++){cin >> v[i];for (int j = V; j >= v[i]; j --)f[j] = max(f[j], f[j - v[i]] + v[i]);}cout << V - f[V] << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

7. 【5.25】P1044 [NOIP 2003 普及组] 栈

题目链接:https://www.luogu.com.cn/problem/P1044

【分析】

  • f[20]存储卡特兰数,初始值为f[0]=1、f[1]=1、f[2]=2。
  • 利用公式f[i] += f[j-1] × f[i-j]枚举最后出栈元素位置,组合左右子问题解。
  • 计算1~n通过栈操作的合法输出序列数,本质是卡特兰数的应用。

【AC_Code】

#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;int f[20] = { 1, 1, 2 };void solve()
{int n; cin >> n;for (int i = 3; i <= n; i ++) for (int j = 1; j <= i; j ++) f[i] += f[j - 1] * f[i - j];cout << f[n] << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve(); return 0;
}

结语
感谢您的阅读!期待您的一键三连!欢迎指正!

在这里插入图片描述

http://www.xdnf.cn/news/8835.html

相关文章:

  • 篇章一 数据结构——前置知识(一)
  • Java 类加载机制详解
  • 【SCL编程案例】1-16整数的随机排列
  • leetcode hot100刷题日记——第一周没做好的题目总结
  • C#拾遗补漏之 Dictionary 详解
  • 【从0到1搞懂大模型】chatGPT 中的对齐优化(RLHF)讲解与实战(9)
  • uniapp报错mongo_cell_decision_not_found
  • Python年快乐!祝福语大全。
  • 从零开始:Python语言进阶之迭代器
  • JVM——JNI 的运行机制
  • Python模型优化技巧
  • Unity基础学习(九)Resources资源同步与异步加载
  • C++23内存分配新特性:std::allocate_at_least
  • JavaWeb:SpringBoot实现简单用户登录JWT用户鉴权
  • string的使用和模拟实现
  • Redis哨兵模式,CLUSTERDOWN Hash slot not server 解决
  • 大数据模型对陌生场景图像的识别能力研究 —— 以 DEEPSEEK 私有化部署模型为例
  • NestJS——重构日志、数据库、配置
  • CMake从入门到实战:现代C++项目构建指南
  • Linux--vim
  • 超简单Translation翻译模型部署
  • TCP/IP
  • Mac系统-最方便的一键环境部署软件ServBay(支持php,java,python,node,go,mysql等)没有之一,已亲自使用!
  • RocketMQ 5.0 核心概念与架构解析
  • 深入剖析 RocketMQ:消息保障、事务处理与负载均衡策略
  • Lua 脚本在 Redis 中的运用-24 (使用 Lua 脚本实现原子计数器)
  • SpringBoot返回xml
  • NV171NV173美光闪存颗粒NV181NV186
  • binlog解析工具——binlog2sql
  • 动态规划(6)下降路径最小值