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

    Coins

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


    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
     
    一看题,以为普普通通一道模版题,结果卡时间卡到哭。。。
     
    注意:当val[i]*num[i]>m时,使用完全背包更快,最开始每晚i注意到这点,通通使用二进制优化,结果超时超哭,然后发现完全背包比转化为二进制优化的01背包更快。
     
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    int val[100005],num[100005];
    int dp[100005],m;
    
    int main()
    {
        int n;
        while(~scanf("%d%d",&n,&m)&&(n+m))
        {
            memset(dp,0,sizeof(dp));
            //dp[0]=1;
            int i;
            for(i=0; i<n; i++)
                scanf("%d",&val[i]);
            for(i=0; i<n; i++)
                scanf("%d",&num[i]);
            for( i=0; i<n; i++)
            {
                int j;
                if(val[i]*num[i]>=m)
                {
                    for( j=val[i];j<=m;j++)
                        dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
                    continue;
                }
                int tmp=1;
                while(num[i]>=tmp)
                {
                    for( j=m;j>=val[i]*tmp;j--)
                        dp[j]=max(dp[j],dp[j-val[i]*tmp]+val[i]*tmp);
                    num[i]-=tmp;
                    tmp=tmp<<1;
                }
                for( j=m;j>=val[i]*num[i];j--)
                    dp[j]=max(dp[j],dp[j-val[i]*num[i]]+val[i]*num[i]);
            }
            int cnt=0;
            for(i=1; i<=m; i++)
                if(dp[i]==i)
                    cnt++;
            printf("%d
    ",cnt);
        }
        return 0;
    }
  • 相关阅读:
    用nodejs的express框架在本机快速搭建一台服务器
    Python版求数组的最大连续区间
    简洁的python测试框架——Croner
    中国有嘻哈——押韵机器人
    服务端测试环境hosts配置检查脚本
    手机客户端软件测试用例设计模板
    【Tomcat】压力测试和优化
    【Tomcat】详解tomcat的连接数与线程池
    【RabbitMQ】2、心得总结,资料汇总
    【高并发解决方案】7、HAProxy安装和配置
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6387343.html
Copyright © 2011-2022 走看看