DSY1531*Bank notes

Description

Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值k求最少要用多少个硬币.

Input

第一行一个数 n, 1 <= n <= 200. 接下来一行 n 个整数b1, b2,..., bn, 1 <= b1 < b2 < ... < b n <= 20 000, 第三行 n 个整数c1, c2,..., cn, 1 <= ci <= 20 000, 表示每种硬币的个数.最后一行一个数k – 表示要凑的面值数量, 1 <= k <= 20 000.

Output

第一行一个数表示最少需要付的硬币数

Sample Input

3
2 3 5
2 2 1
10

Sample Output

3
 
多重背包,转化为01背包做,但是需要加上二进制优化,不然会超时。
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 using namespace std;
 6 int f[22222]={0},b[222]={0},c[222]={0},s[4000005]={0},w[4000005]={0};
 7 
 8 int main()
 9 {
10     int n=0;
11     cin>>n;
12     for (int i=1;i<=n;i++)
13       cin>>b[i];
14     for (int i=1;i<=n;i++)
15       cin>>c[i];
16     int tot=0;
17     int now=1,x=0;
18     while (now<=n)
19     {
20         x=1;
21         while (c[now]-x>0)
22         {
23             ++tot;
24             s[tot]=x*b[now];
25             w[tot]=x;
26             c[now]-=x;
27             x=x*2;
28         }
29         if (c[now]>0)
30         {
31             ++tot;
32             s[tot]=c[now]*b[now];
33             w[tot]=c[now];
34         }
35         ++now;
36     }
37     int k=0;
38     memset(f,10000,sizeof(f));
39     cin>>k;
40     f[0]=0;
41     for (int i=1;i<=tot;i++)
42       for (int j=k;j>=s[i];j--)
43         f[j]=min(f[j-s[i]]+w[i],f[j]);
44     cout<<f[k];
45     return 0;
46 }
原文地址:https://www.cnblogs.com/Maxxzy/p/6225092.html