【HDOJ】2844 Coins
2014-05-26 21:38
204 查看
完全背包。
#include <stdio.h> #include <string.h> int a[105], c[105]; int n, m; int dp[100005]; int mymax(int a, int b) { return a>b ? a:b; } void CompletePack(int c) { int i; for (i=c; i<=m; ++i) dp[i] = mymax(dp[i], dp[i-c]); } void ZeroOnePack(int c) { int i; for (i=m; i>=c; --i) dp[i] = mymax(dp[i], dp[i-c]); } void multipack(int c, int n) { int k; if (c*n >= m) { CompletePack(c); return ; } k = 1; while (k < n) { ZeroOnePack(k*c); n -= k; k *= 2; } ZeroOnePack(n*c); } int main() { int i, total; while (scanf("%d%d",&n,&m)!=EOF && (n||m)) { memset(dp, 0, sizeof(dp)); dp[0] = 1; for (i=1; i<=n; ++i) scanf("%d", &a[i]); for (i=1; i<=n; ++i) scanf("%d", &c[i]); for (i=1; i<=n; ++i) multipack(a[i], c[i]); total = 0; for (i=1; i<=m; ++i) if (dp[i]) ++total; printf("%d\n", total); } return 0; }
相关文章推荐
- 暑期dp46道(37) HDOJ 2844 COINS
- HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)
- hdoj-2844-Coins【多重背包】
- HDOJ2844-Coins(DP)
- hdoj 2844 Coins【多重背包】【dp】
- hdoj 2844 Coins(多重背包)
- hdoj 2844 Coins
- HDOJ题目2844 Coins(二维多重背包)
- HDOJ 2844 Coins
- HDOJ 2401 Baskets of Gold Coins (数学题)
- hdu 2844 Coins(多重背包优化)
- hdu_2844_Coins(多重背包)
- HDU 2844 Coins 多重背包 (重量和价值相同)
- HDU 2844 Coins(DP 背包)
- poj 1742 && hdu 2844 Coins
- [ACM] hdu 2844 Coins (多重背包)
- hdu 2844 Coins(多重背包)
- HDU 2844(Coins)多重背包
- HDU 2844 Coins (组合背包)
- 【HDU 2844】Coins(多重背包)