1022. 宠物小精灵之收服

思路

双层dp

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

const int N = 1010, mod = 1e9 + 7;

int n, m, k, x, y, z, ans, t;
int w[N], f[N][N];

void solve()
{
	cin >> n >> m >> k;
	
	for (int i = 1; i <= k; i ++ )
	{
		cin >> x >> y;
		for (int j = n; j >= x; j -- )
		{
			for (int l = m - 1; l >= y; l -- )
			{
				f[j][l] = max(f[j][l], f[j - x][l - y] + 1);
			}
		}
	}
	
	cout << f[n][m - 1] << " ";
	
	ans = m - 1;
	
	while (ans > 0 && f[n][ans - 1] == f[n][m - 1]) ans --;
	
	cout << m - ans << "\n";
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T = 1;
//	cin >> T;
	while (T -- )
	{
		solve();
	}
}
相关推荐
Yupureki1 分钟前
《算法竞赛从入门到国奖》算法基础:动态规划-基础线性dp
c语言·开发语言·算法·动态规划
Allen_LVyingbo43 分钟前
自进化医疗智能体:动态记忆与持续运行的Python架构编程(上)
数据结构·python·架构·动态规划·健康医疗
老鼠只爱大米2 小时前
LeetCode经典算法面试题 #45:跳跃游戏II(贪心法、动态规划、BFS等多种实现方案详解)
算法·leetcode·贪心算法·动态规划·bfs·java面试·跳跃游戏ii
Boop_wu13 小时前
[Java 算法] 动态规划(1)
算法·动态规划
云淡风轻~窗明几净18 小时前
关于TSP的海岸线猜想:SeaLine算法的逐层法(不同于逐点法)
数据结构·算法·动态规划·模拟退火算法
hnjzsyjyj20 小时前
洛谷 P1192:台阶问题 ← 动态规划 + 前缀和优化
前缀和·动态规划·差分
cpp_25011 天前
P8395 [CCC 2022 S1] Good Fours and Good Fives
数据结构·c++·算法·动态规划·图论·题解·洛谷
VelinX1 天前
【个人学习||算法】动态规划
学习·算法·动态规划
Boop_wu1 天前
[Java 算法] 动态规划2
算法·leetcode·动态规划
Allen_LVyingbo1 天前
“拓扑量子计算被证伪”科学纠偏事件分析
算法·搜索引擎·全文检索·动态规划·创业创新·量子计算