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();
	}
}
相关推荐
忘梓.6 小时前
解锁动态规划的奥秘:从零到精通的创新思维解析(9)
算法·动态规划·代理模式
SsummerC7 小时前
【leetcode100】杨辉三角
python·leetcode·动态规划
Nigori7_3 天前
day32-动态规划__509. 斐波那契数__70. 爬楼梯__746. 使用最小花费爬楼梯
算法·动态规划
Nigori7_3 天前
day33-动态规划__62.不同路径__63. 不同路径 II __343. 整数拆分__343. 整数拆分
算法·动态规划
刃神太酷啦3 天前
基础算法篇(5)(蓝桥杯常考点)—动态规划(C/C++)
数据结构·c++·算法·leetcode·蓝桥杯·动态规划·蓝桥杯c++组
冲帕Chompa3 天前
代码随想录动态规划part02
算法·动态规划
小羊在奋斗3 天前
【今日三题】经此一役小红所向无敌(模拟) / 连续子数组最大和(动态规划) / 非对称之美(贪心)
算法·动态规划
阳洞洞3 天前
leetcode 139. Word Break
算法·leetcode·word·动态规划
ChoSeitaku4 天前
NO.88十六届蓝桥杯备战|动态规划-多重背包|摆花(C++)
c++·蓝桥杯·动态规划
阳洞洞5 天前
leetcode 2787. Ways to Express an Integer as Sum of Powers
算法·leetcode·动态规划·01背包问题