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

    Coins

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


    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
     

    题意:给定n种硬币,每种价值是a,数量是c,让你求不大于给定V的不同的价值数,就是说让你用这些硬币来组成多少种不同的价格,并且价格不大于V。

    析:一看就应该知道是一个动态规划的背包问题,只不过是变形,那我们就统计不大于V的不同价格数,也容易实现,

    对于多重背包我们是把它转化为01背包和完全背包来解决的。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4  
     5 using namespace std;
     6 typedef long long LL;
     7 const int maxn = 100000 + 10;
     8 int d[maxn];
     9 int c[maxn], a[maxn], V;
    10  
    11 void zeroonepack(int v, int val){
    12     for(int i = V; i >= v; --i)
    13         d[i] = max(d[i], d[i-v]+val);
    14 }
    15  
    16 void completepack(int v, int val){
    17     for(int i = v; i <= V; ++i)
    18         d[i] = max(d[i], d[i-v]+val);
    19 }
    20  
    21 void multiplepack(int v, int val, int num){
    22     if(V <= num * v){  completepack(v, val);  return ; }
    23  
    24     int k = 1;
    25     while(k <= num){
    26         zeroonepack(v*k, val*k);
    27         num -= k;
    28         k <<= 1;
    29     }
    30     zeroonepack(num*v, num*val);
    31 }
    32  
    33 int main(){
    34     int n;
    35     while(scanf("%d %d", &n, &V)){
    36         if(!n && !V)  break;
    37         for(int i = 0; i < n; ++i)  scanf("%d", &a[i]);
    38         for(int i = 0; i < n; ++i)  scanf("%d", &c[i]);
    39  
    40         memset(d, 0, sizeof(d));
    41         for(int i = 0; i < n; ++i)
    42             multiplepack(a[i], a[i], c[i]);
    43  
    44         int cnt = 0;
    45         for(int i = 1; i <= V; ++i)
    46             if(d[i] == i)  ++cnt;
    47         printf("%d
    ", cnt);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    数据结构之 内部排序---交叉排序(没啥特别的!!!)
    数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)
    数据结构之 图论---图的深度遍历( 输出dfs的先后遍历序列 )
    数据结构之 排序---折半插入排序(时间复杂度 O(nlog2 n) )
    HDU 1022 之 Train Problem I
    Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)
    Bestcoder round 18----B题(一元三次方程确定区间的最大值(包含极值比较))
    操作字典
    在线压缩图片
    JSON转C#实体类
  • 原文地址:https://www.cnblogs.com/gongpixin/p/6733768.html
Copyright © 2011-2022 走看看