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();
	}
}
相关推荐
老鼠只爱大米1 天前
LeetCode算法题详解 42:接雨水
leetcode·动态规划·双指针·单调栈·接雨水·雨水收集
闻缺陷则喜何志丹1 天前
【C++动态规划 状压dp】1879. 两个数组最小的异或值之和|2145
c++·算法·动态规划·力扣·数组·最小·动态规范
leoufung2 天前
LeetCode 221:Maximal Square 动态规划详解
算法·leetcode·动态规划
好易学·数据结构2 天前
可视化图解算法77:零钱兑换(兑换零钱)
数据结构·算法·leetcode·动态规划·力扣·牛客网
Tisfy2 天前
LeetCode 1458.两个子序列的最大点积:动态规划
算法·leetcode·动态规划·题解·dp
会员果汁3 天前
leetcode-动态规划-买卖股票
算法·leetcode·动态规划
leoufung3 天前
LeetCode动态规划经典题:Unique Paths 网格路径计数详解
算法·leetcode·动态规划
放荡不羁的野指针4 天前
leetcode150题-动态规划
算法·动态规划
水月wwww4 天前
【算法设计】动态规划
算法·动态规划
学海一叶4 天前
论文精读-《ReAct: Synergizing Reasoning and Acting in Language Models》,2022
论文阅读·人工智能·语言模型·动态规划·agent