[CF189A]Cut Ribbon(完全背包,DP)
题目链接:http://codeforces.com/problemset/problem/189/A
题意:给你长为n的绳子,每次只允许切a,b,c三种长度的段,问最多能切多少段。注意每一段都得是a,b,c中长度的一种。
解法:这个题可以看作是完全背包,但是由于切割长度有限制,所以要做一些调整。我们初始化dp(i)为长度为i时的最优解。一开始dp(a)=dp(b)=dp(c)=1是显而易见的,我们转移的起点也是这里。我们不希望枚举到不符合条件的情况,所以多一步判断:dp[j-w[i]] != 0
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <cassert> 24 #include <cstdio> 25 #include <bitset> 26 #include <vector> 27 #include <deque> 28 #include <queue> 29 #include <stack> 30 #include <ctime> 31 #include <set> 32 #include <map> 33 #include <cmath> 34 using namespace std; 35 #define fr first 36 #define sc second 37 #define cl clear 38 #define BUG puts("here!!!") 39 #define W(a) while(a--) 40 #define pb(a) push_back(a) 41 #define Rint(a) scanf("%d", &a) 42 #define Rll(a) scanf("%I64d", &a) 43 #define Rs(a) scanf("%s", a) 44 #define Cin(a) cin >> a 45 #define FRead() freopen("in", "r", stdin) 46 #define FWrite() freopen("out", "w", stdout) 47 #define Rep(i, len) for(LL i = 0; i < (len); i++) 48 #define For(i, a, len) for(LL i = (a); i < (len); i++) 49 #define Cls(a) memset((a), 0, sizeof(a)) 50 #define Clr(a, x) memset((a), (x), sizeof(a)) 51 #define Fuint(a) memset((a), 0x7f7f, sizeof(a)) 52 #define lrt rt << 1 53 #define rrt rt << 1 | 1 54 #define pi 3.14159265359 55 #define RT return 56 #define lowbit(x) x & (-x) 57 #define onenum(x) __builtin_popcount(x) 58 typedef long long LL; 59 typedef long double LD; 60 typedef unsigned long long Uint; 61 typedef pair<LL, LL> pii; 62 typedef pair<string, LL> psi; 63 typedef map<string, LL> msi; 64 typedef vector<LL> vi; 65 typedef vector<LL> vl; 66 typedef vector<vl> vvl; 67 typedef vector<bool> vb; 68 69 const int maxn = 4040; 70 int dp[maxn]; 71 int n; 72 int w[5]; 73 74 int main() { 75 // FRead(); 76 Cls(dp); 77 Rint(n); 78 For(i, 1, 4) { 79 Rint(w[i]); 80 dp[w[i]] = 1; 81 } 82 For(i, 1, 4) { 83 For(j, w[i], n+1) { 84 bool flag = 1; 85 if(dp[j-w[i]]) { 86 dp[j] = max(dp[j], dp[j-w[i]]+1); 87 } 88 } 89 } 90 printf("%d\n", dp[n]); 91 RT 0; 92 }