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
     
  • 相关阅读:
    java kafka单列模式生产者客户端
    手机视频加手机壳小软件
    Jetbrains系列产品(Pycharm IDEA)2020.1及以下版本最新安装方法
    快速解决网页不能复制问题
    MATLAB 2018a软件安装教程
    有哪些超级良心的电脑软件,让你相见恨晚?
    SPSS 25安装资源及其安装教程!!
    SPSS 25 Mac版安装资源及其安装教程!!
    IDE | Pycharm 2020.1.3 安装教程
    MATLAB 2018a软件安装教程
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/8835137.html
Copyright © 2011-2022 走看看