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();
	}
}
相关推荐
lvwangshu4 小时前
CF1882D Tree XOR 题解
动态规划·题解·贪心·dp·树形dp·换根dp·性质题·思维好题·推导题
Interview Aid1128 小时前
Snowflake OA 面试真题:前缀和、BFS 与动态规划
面试·动态规划·宽度优先
wzdark4 天前
动态规划算法的状态转移矩阵可视化分析5
算法·矩阵·动态规划
所以遗憾是什么呢?4 天前
【题解】Educational Codeforces Round 195 (Rated for Div. 2) ABCDE
c++·动态规划·acm·icpc·ccpc·xcpc·codeforecs
lvwangshu4 天前
川剧复演 题解
动态规划·题解·技巧·性质题·思维好题
All for pursuit.5 天前
【动态规划-21】312.戳气球
数据结构·c++·算法·leetcode·动态规划
y1su5 天前
Leetcode 动态规划 模板
java·数据结构·算法·leetcode·动态规划
w_zero_one6 天前
动态规划(三)
java·算法·动态规划
木井巳6 天前
【动态规划】三步问题
java·算法·leetcode·动态规划·推荐算法
浅念-6 天前
动态规划:线性DP——多状态DP与子数组专题笔记
c++·笔记·算法·leetcode·职场和发展·动态规划·力扣