Bootstrap

1022. 宠物小精灵之收服

思路

双层dp

代码

#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();
	}
}

;