zoukankan      html  css  js  c++  java
  • Poj2844Coins(多重背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2844

    Coins

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

    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种硬币,最大价值m
         n种硬币的面值 ,
          n种硬币对应的数量
         求出在 1 -- m 区间内  能用这些硬币表达的 数值有多少个
      思路:多重背包 , 初始化条件 除 0 cost 之外都是 -INF
         最后统计数值大于零(可以表达)的个数
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    
    using namespace std ; 
    
    #define maxn 110000
    #define INF 100000000
    
    int n , m ; 
    int a[maxn] ; 
    int c[maxn] ; 
    int dp[maxn] ;
    
    void CompletePack(int cost , int value){
        for(int i=cost ; i<=m ; i++){
            dp[i] = max(dp[i] , dp[i-cost] + value) ; 
        }
    }
    
    void ZeroOnePack(int cost , int value ){
        for(int i = m ; i>=cost ; i--){
            dp[i] = max(dp[i] , dp[i-cost] + value) ; 
        }
    }
    
    void MultiplePack(int cost , int value , int acount ){
        if(cost*acount>=m){
            CompletePack(cost , value) ; 
        }else{
            int num = 1 ;
            while(num<acount){
                ZeroOnePack(cost*num , value*num ) ; 
                acount -= num ; 
                num *= 2 ; 
            } 
            ZeroOnePack(cost*acount , value*acount) ; 
        }
    }
    
    int main(){
    
        while(~scanf("%d%d" , &n , &m)){
            if(n == 0 && m == 0 ){
                break ; 
            }
    
            for(int i=1 ; i<=n ; i++){
                scanf("%d" ,&a[i]) ; 
            }
            for(int i=1 ; i<=n ; i++){
                scanf("%d" ,&c[i]) ; 
            //    dp[i] = -INF ; 
            }
            for(int i=1 ; i<=m ; i++){
                dp[i] = -INF ; 
            }
            dp[0] = 0 ; 
    
            for(int i=1 ; i<=n ; i++){
                MultiplePack(a[i] , a[i] , c[i]) ;
    
            }
    
            int sum = 0 ; 
    
            for(int i=1 ; i<=m ; i++){
                if(dp[i]>0){
                    sum++ ; 
                    //cout<<i<<endl ; 
                }
            }
            printf("%d
    " , sum) ; 
        }
    
        return 0 ; 
    }
    View Code
     
  • 相关阅读:
    hdu 1524
    hdu 1536
    转载一篇博弈博弈入门的好文章
    nim 博弈
    WPF 从Main函数启动
    C# map network drive sample
    Predicate 学习
    WPF 绑定到附加属性 绑定到只读属性
    WPF 带有watermark的文本输入框
    使用Windows服务发布WCF服务
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/8835137.html
Copyright © 2011-2022 走看看