Coins
Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
Sample Output
8 4
解题报告:这题是多重背包,方法有几种。我写了三种方法。
第一种是利用w=v的特点,用一个cnt[j]数组表示达到j状态i物品的使用次数,注意cnt每一轮都要初始化为0。dp用来表示当前价值是否可以达到(0不可达,1可达)。
#include <stdio.h>
#include <string.h>
int dp[100005], v[105],num[105], cnt[100005];
int main()
{
int n, m, i, j;
while(scanf("%d%d", &n, &m) != EOF && n+m){
int ans = 0;
memset(dp, 0, sizeof(dp)), dp[0] = 1;
for (i = 0; i < n; i ++){
scanf("%d", v + i);
}
for (i = 0; i < n; i ++){
scanf("%d", num + i);
}
for (i = 0; i < n; i ++){
memset(cnt, 0, sizeof(cnt));
for (j = v[i]; j <= m; j ++){
if(dp[j-v[i]] == 1 && dp[j] == 0 && cnt[j-v[i]] < num[i]){
cnt[j] = cnt[j-v[i]] + 1;
dp[j] = 1;
ans ++;
}
}
}
printf("%d\n", ans);
}
return 0;
}
方法二是利用多重背包的二进制优化,比如k物品有ki个,那么可以把ki转换为二进制,然后把二进制位权(从1到ki可以达到的最大位的位权)乘以它的价值和体积组合得到一个新的物品,不要漏了ki本身,如果它不是刚好被分解的话。
#include <stdio.h>
#include <string.h>
#include <algorithm>
int dp[100005], weight[105], num[105];
int V, n;
void ZeroOnePack(int cost) {
for (int i = V; i >= cost; i --) {
dp[i] = std::max(dp[i], dp[i- cost]);
}
}
void CompletePack(int cost) {
for (int i = cost; i <= V; i ++) {
dp[i] = std::max(dp[i], dp[i - cost]);
}
}
void MultiPack(int cost, int n) {
if(cost * n >= V - cost + 1) {
CompletePack(cost);
return ;
}
int k = 1;
while(k < n) {
ZeroOnePack(cost * k);
n -= k;
k *= 2;
}
ZeroOnePack(cost * n);
}
int main() {
while (scanf("%d%d", &n, &V) != EOF, V + n) {
int ans = 0;
memset(dp, 0, sizeof(dp)), dp[0] = 1;
for (int i = 0; i < n; i ++) {
scanf ("%d", weight + i);
}
for (int i = 0; i < n; i ++) {
scanf ("%d", num + i);
}
for (int i = 0; i < n; i ++) {
MultiPack(weight[i], num[i]);
}
for (int i = 1; i <= V; i ++) {
if(dp[i]) {
ans ++;
}
}
printf("%d\n", ans);
}
return 0;
}
第三种方法其实也是多重背包,不过用的是单调队列的方法,传说中的O(NM)我也是依葫芦画瓢。
但是有两点问题,一个是用deque慢了不只一点点。
另一点是虽然是O(NM)算法,还是不能完全抛弃01背包和完全背包的那一部分判断。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <deque>
int dp[100005], weight[105], num[105];
int V, n;
int ans = 0;
void ZeroOnePack(int cost) {
for (int i = V; i >= cost; i --) {
if (dp[i-cost] && !dp[i]) {
dp[i] = 1;
ans ++;
}
}
}
void CompletePack(int cost) {
for (int i = cost; i <= V; i ++) {
if (dp[i-cost] && !dp[i]) {
dp[i] = 1;
ans ++;
}
}
}
void MultiPack(int cost, int n) {
if(n == 1) {
ZeroOnePack(cost);
return ;
}
if(cost * n >= V - cost + 1) {
CompletePack(cost);
return ;
}
// std::deque<int> md;
int md[100005];
for (int j = 0; j < cost; j ++) {
int sum = 0;
int qb = 0, qe = -1;
// md.clear();
for (int k = j; k <= V; k += cost) {
// if(md.size() == n + 1) {
// sum -= md.front();
// md.pop_front();
// }
// md.push_back(dp[k]);
// sum += dp[k];
if(qe - qb == n) {
sum -= md[qb++];
}
md[++qe] = dp[k];
sum += dp[k];
if(!dp[k] && sum != 0){
dp[k] = 1;
ans ++;
}
}
}
}
int main() {
while (scanf("%d%d", &n, &V) != EOF, V + n) {
ans = 0;
memset(dp, 0, sizeof(dp)), dp[0] = 1;
for (int i = 0; i < n; i ++) {
scanf ("%d", weight + i);
}
for (int i = 0; i < n; i ++) {
scanf ("%d", num + i);
}
for (int i = 0; i < n; i ++) {
MultiPack(weight[i], num[i]);
}
printf("%d\n", ans);
}
return 0;
}