• hdu 2844 Coins 多重背包(模板) *


    Coins

                                                                                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    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
     
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<sstream>
    #include<cstdlib>
    #include<string>
    #include<string.h>
    #include<cstring>
    #include<algorithm>
    typedef long long ll;
    #define INF 0x3f3f3f3f  
    using namespace std;
    #define maxn 100005
    int a[maxn],c[maxn],dp[maxn];
    int main(){
       int n,m; 
       while(cin >> n >> m){
               if(!n && !m){
                   break;    
            }
            for(int i=0;i<n;i++){
                cin >> a[i];//a[i]既是物体的体积,又是物体的价值
            }
            for(int i=0;i<n;i++){
                cin >> c[i]; //c[i]是物体的数量
            }
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++){
                if(a[i]*c[i]>=m){//体积乘以数量大于总体积,说明不能完全装完,相当于有无穷件,用完全背包
                    for(int j=a[i];j<=m;j++){
                        dp[j] = max(dp[j],dp[j-a[i]]+a[i]);
                    }
                }
                else{//可以装完,用01背包
                    int k = 1;
                    while(k<c[i]){//二进制优化
                        for(int j=m;j>=a[i]*k;j--){
                            dp[j] = max(dp[j],dp[j-a[i]*k]+a[i]*k);
                        }
                        c[i] -= k;
                        k *= 2;
                    }
                    for(int j=m;j>=a[i]*c[i];j--){
                        dp[j] = max(dp[j],dp[j-a[i]*c[i]]+a[i]*c[i]);
                    }
                }
            }
            int count = 0;//计数
            for(int i=1;i<=m;i++){
                if(dp[i] == i){
                    count++;//可以组合且不用找钱
                }
            }
            cout << count << endl;
       }
       return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    vue脚手架项目配置后端接口(mock数据)
    list添加删除动画(transition-group)
    fast mock官网mock数据
    vuex日志插件(createLogger)
    环形进度条
    程序员的周末生活
    hbase错误之Region in transition异常解决
    不同虚拟机之间的文件传递
    windows上链接虚拟机
    hadoop——yarn
  • 原文地址:https://www.cnblogs.com/l609929321/p/7243366.html
Copyright © 2020-2023  润新知