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();
	}
}
相关推荐
_dindong2 天前
动规:回文串问题
笔记·学习·算法·leetcode·动态规划·力扣
是那盏灯塔3 天前
【算法】——动态规划算法及实践应用
数据结构·c++·算法·动态规划
橘颂TA3 天前
【剑斩OFFER】算法的暴力美学——将 x 减到零的最小操作数
c++·算法·leetcode·动态规划
十八岁讨厌编程3 天前
【算法训练营Day30】动态规划part6
算法·动态规划
_dindong4 天前
动规:01背包
数据结构·笔记·学习·算法·leetcode·动态规划·力扣
Espresso Macchiato4 天前
Leetcode 3700. Number of ZigZag Arrays II
动态规划·leetcode hard·矩阵乘法·leetcode 3700·leetcode周赛469
未知陨落5 天前
LeetCode:84.完全平方数
算法·leetcode·动态规划
dragoooon345 天前
[优选算法专题三.二分查找——NO.24搜索旋转排序数组中的最⼩值]
数据结构·leetcode·动态规划
十八岁讨厌编程7 天前
【算法训练营Day27】动态规划part3
算法·动态规划
Excuse_lighttime8 天前
除自身以外数组的乘积
java·数据结构·算法·leetcode·eclipse·动态规划