Cut Ribbon(洛谷)

题目翻译
给一长度为 n 的缎带,要求将其剪成若干长度为 a, b, c 的缎带,且缎带数量尽可能多。

输入格式
输入仅一行,四个正整数 n, a, b, c

输出格式
输出仅一行,即缎带数量的最大值。

输入样例1
5 5 3 2

输出样例1
2

输入样例2
7 5 5 2

输出样例2
2

数据范围
n, a, b, c ≤ 4000


题解
完全背包(优化2.0):

#include <iostream>
#include <cstring>
using namespace std;

const int N = 4010;

int n;
int v[4], f[N];

int main()
{
    
    
	cin >> n >> v[1] >> v[2] >> v[3];
	
	memset(f, -0x3f, sizeof f);
	
	f[0] = 0;
	for (int i = 1; i <= 3; i ++)
		for (int j = v[i]; j <= n; j ++)
			f[j] = max(f[j], f[j - v[i]] + 1);
			
	cout << f[n] << endl;
	return 0;		
}

猜你喜欢

转载自blog.csdn.net/weixin_46239370/article/details/108908059
cut