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;
    }
  • 相关阅读:
    移动端web app开发备忘
    HDU 5391-Zball in Tina Town(数论)
    LeetCode:Invert Binary Tree
    Mongo集群之主从复制
    Cocos2d-x--iOS平台lua加密成luac资源方法和Jsc文件&lt;MAC平台开发试用--windows平台暂未研究&gt;
    优秀程序猿因何而优秀?
    Java Exception和Error的差别
    【Android开发】之Fragment与Acitvity通信
    Draw the RGB data from kinect C++ via opengl
    使用Opencv2遇到error C2061: 语法错误: 标识符dest
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6387343.html
Copyright © 2011-2022 走看看