• 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
     
  • 相关阅读:
    个人便签
    秒杀系统架构分析与实战
    NPOI大数据分批写入同个Excel
    js获取鼠标坐标位置兼容多个浏览器
    月薪3万的程序员都避开了哪些坑
    怎样理解阻塞非阻塞与同步异步的区别?
    JS中的prototype
    互联网——降级论
    fedora自带OpenJDK,所以如果安装官方的JDK的话要先删除OpenJDK
    cygwin 安装完后不能进入think问题,网上99%都是错误的
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/8835137.html
Copyright © 2020-2023  润新知