zoukankan      html  css  js  c++  java
  • HDU 2844 二进制优化的多重背包

    Coins

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 11596    Accepted Submission(s): 4634


    Problem 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.
     
    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
     
    Source
     
     
    题意:有不同面值的 相应数量不同n种硬币  问1~m元的价格有哪些 可以由硬币组成 f[i]==i
     
    题解: 多重背包    二进制优化
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<map>
     5 #include<queue>
     6 #include<stack>
     7 using namespace std;
     8 int n,m;
     9 struct node
    10 {
    11     int a,c; 
    12 } N[105];
    13 int f[110005];
    14 int ans;
    15 int value[100005],size[100005];
    16 int count;
    17 void slove(int q)
    18 {
    19     count=1;
    20    for(int i=1;i<=q;i++) 
    21   {  
    22      int c=N[i].c,v=N[i].a;   
    23     for (int k=1; k<=c; k<<=1) 
    24     {  
    25         value[count] = k*v;  
    26         size[count++] = k*v;  
    27         c -= k;  
    28     }  
    29     if (c > 0)  
    30     {  
    31        value[count] = c*v;  
    32        size[count++] = c*v;  
    33     }  
    34 }  
    35 }
    36 int main()
    37 {
    38     while(scanf("%d %d",&n,&m)!=EOF)
    39     {
    40         if(n==0&&m==0)
    41            break;
    42         for(int i=0; i <= m; i++)
    43             f[i] = 0;
    44         ans=0;
    45         for(int i=1;i<=n;i++)
    46         scanf("%d",&N[i].a);
    47         for(int i=1;i<=n;i++)
    48         scanf("%d",&N[i].c);
    49         slove(n);
    50         for(int i=1;i<count;i++)
    51          for(int gg=m;gg>=size[i];gg--)
    52           f[gg]=max(f[gg],f[gg-size[i]]+value[i]);
    53         for(int i=1;i<=m;i++)
    54           if(f[i]==i)
    55           ans++;
    56         cout<<ans<<endl;
    57     }
    58     return 0;
    59 }
     
  • 相关阅读:
    ecshop里的$_CFG从哪来的
    高效PHP程序必知的53个技巧
    Jquery结合datagrid框架
    PHP数据类型转换(字符转数字,数字转字符)
    php 操作数组 (合并,拆分,追加,查找,删除等)
    css3制作导航栏
    php日期转时间戳,指定日期转换成时间戳
    PHP 时间与字符串的相互转化
    php 生成.txt文件
    linux PHP yum 安装phpzie
  • 原文地址:https://www.cnblogs.com/hsd-/p/5447932.html
Copyright © 2011-2022 走看看