Cut Ribbon
Posted dealer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cut Ribbon相关的知识,希望对你有一定的参考价值。
Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:
- After the cutting each ribbon piece should have length a, b or c.
- After the cutting the number of ribbon pieces should be maximum.
Help Polycarpus and find the number of ribbon pieces after the required cutting.
The first line contains four space-separated integers n, a, b and c (1 ≤ n, a, b, c ≤ 4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers a, b and c can coincide.
Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.
5 5 3 2
2
7 5 5 2
2
In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3.
In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 2.
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <unordered_set> #include <unordered_map> #define ll long long #define mod 998244353 using namespace std; int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} }; int main() { int n; cin >> n; vector<int> a(3),dp(n+1); for (int i = 0; i < 3; i++) cin >> a[i]; sort(a.begin(), a.end()); if (a[0] <= n) dp[a[0]] = 1; if (a[1] <= n) dp[a[1]] = 1; if (a[2] <= n) dp[a[2]] = 1; for (int i = 1; i <= n; i++) { int ans = dp[i]; if (i > a[0] && dp[i - a[0]] != 0) ans = max(ans, dp[i - a[0]] + 1); if (i > a[1] && dp[i - a[1]] != 0) ans = max(ans, dp[i - a[1]] + 1); if (i > a[2] && dp[i - a[2]] != 0) ans = max(ans, dp[i - a[2]] + 1); dp[i] = ans; } cout << dp[n] << endl; return 0; }
以上是关于Cut Ribbon的主要内容,如果未能解决你的问题,请参考以下文章