zoukankan      html  css  js  c++  java
  • HDU-2844 Coins (多重背包)

    题目:

    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.

    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


    • 多重背包问题,要求每个背包恰好装满,因此把dp[0]赋为0,其他赋值负无穷。
    • 一开始忘记二进制优化最后要把剩余的数量再做一次01背包,因此老是漏掉2*1的解。
    • 觉得自己把所有东西都写在一块太难看了,因此借鉴了kuangbin大神的模板,瞬间清爽多了。

    This is Code:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define M(a, b) memset(a, b, sizeof(a))
     4 #define INF -0x3f3f3f3f
     5 int val[100], num[100], dp[100005], n, m;
     6 
     7 void ZeroOnePack(int cost, int weight){
     8     for (int i = m; i >= cost; i--)
     9         dp[i] = max(dp[i-cost]+weight, dp[i]);
    10 }
    11 
    12 void CompletePack(int cost, int weight){
    13     for (int i = cost; i <= m; i++)
    14         dp[i] = max(dp[i-cost]+weight, dp[i]);
    15 }
    16 
    17 void MultiplePack(int cost, int weight, int amount){
    18     if (cost*amount >= m) CompletePack(cost, weight);
    19     else{
    20         int k = 1;
    21         while(k < amount){
    22             ZeroOnePack(k*cost, k*weight);
    23             amount -= k;
    24             k <<= 1;
    25         }
    26         ZeroOnePack(amount*cost, amount*weight);//不要漏解
    27     }
    28 }
    29 
    30 int main()
    31 {
    32     while(scanf("%d%d", &n, &m), n || m){
    33         M(val, 0);
    34         M(num, 0);
    35         for (int i = 1; i <= m; i++) dp[i] = INF;
    36         dp[0] = 0;
    37         int maxn = 0;
    38         for (int i = 0; i < n; i++) scanf("%d", &val[i]);
    39         for (int i = 0; i < n; i++){
    40             scanf("%d", &num[i]);
    41             maxn += val[i] * num[i];
    42         }
    43         m = min(maxn, m);//背包容量取预计最大金额和所有硬币最大金额的最小值
    44         for (int i = 0; i < n; i++){
    45             MultiplePack(val[i], val[i], num[i]);
    46         }
    47         int ans = 0;
    48         for (int i = 0; i <= m ; i++)
    49             if(dp[i] > 0) ++ans;
    50 
    51         printf("%d
    ", ans);
    52     }
    53 
    54     return 0;
    55 }
  • 相关阅读:
    文件的序列化和反序列化
    三个小功能,游戏倒计时,文件的序列化和反序列化,txt文档的读取和写入
    Unity 中Debug打印的全局注释方式和重写
    导航制作的几个步骤
    Unity中删除文件目录下的所有文件和查看文件里面的内容
    VS2017一些小技巧
    在Unity中图标进行鼠标图标更换
    Electron-Vue 使用 oss 实现上传、下载
    Electron-Vue 调用本地数据库
    构建 Electron-Vue 脚手架项目
  • 原文地址:https://www.cnblogs.com/robin1998/p/6359134.html
Copyright © 2011-2022 走看看