• poj 1742(好题,楼天城男人八题,混合背包)


    Coins
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 33269   Accepted: 11295

    Description

    People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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种 硬币,第i种价值为 A[i],数量为C[i] ,问这些硬币能够组成1-m中哪些硬币。

    分析:这题去年在hdu 2844上面过了,利用分组背包每次将每种物品分割,分割完一次马上进行就进行 01 背包,这样处理的速度就比所有的都分割完后再进行01背包要快许多。。但是没想到poj的数据如此变态。。然后看了网上的题解,恍然大悟。。原来当A[i]*C[i]>=m时,我们就可以将第i件物品看成无穷件了。。然后只要进行完全背包就OK。

    hdu 2844 AC代码:
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    using namespace std;
    
    int  a[105],b[105],value[100000],f[100000];
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF&&n>0)
        {
            int wi=0;
            memset(f,0,sizeof(f));
            for(int i=0; i<n; i++)
            {
                scanf("%d",&a[i]);
            }
            for(int i=0; i<n; i++)
            {
                scanf("%d",&b[i]);
            }
            for(int i=0; i<n; i++)
            {
                    int sum=1;
                    int w=b[i],v=a[i];
                    while(w>0)
                    {
                        if(w>=sum)
                        {
                            value[wi]=sum*v;
                            w=w-sum;
                            sum=sum*2;
                        }
                        else
                        {
                            value[wi]=w*v;
                            w-=w;
                        }
    
                        for(int j=m; j>=value[wi]; j--)
                            f[j]=max(f[j],f[j-value[wi]]+value[wi]);
                            wi++;
                    }
    
    
    
            }
            int ans=0;
            for(int i=1; i<=m; i++)
            {
               if(f[i]==i) ans++;
            }
            printf("%d
    ",ans);
    
        }
        return 0;
    }
    View Code

     poj 1742 AC代码:

    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    #define N 1005
    #define M 100005
    using namespace std;
    
    int  A[105],C[105],W[N];
    bool dp[M];
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF,n+m)
        {
            memset(dp,0,sizeof(dp));
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&A[i]);
            }
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&C[i]);
            }
            int k = 1;
            dp[0]=1;  ///初始化0元是存在的
            int ans = 0;
            for(int i=1; i<=n; i++)
            {
                if(C[i]*A[i]>=m) {  ///如果A[i]*C[i] >= m,我们可以将其当成完全背包问题处理,因为k*A[i]不能超过
                                    ///m,超过了就可以看成"无限"了.
                    for(int v = A[i];v<=m;v++){
                        if(!dp[v]&&dp[v-A[i]])  {
                            dp[v]=true;
                            ans++;
                        }
                    }
                }else{
                    int t = 1;
                    while(C[i]>0){
                        if(C[i]>=t){
                            W[k]=A[i]*t;
                            C[i]-=t;
                            t = t<<1;
                        }else{W[k]=A[i]*C[i];C[i]=0;}
                        for(int v=m;v>=W[k];v--){
                            if(!dp[v]&&dp[v-W[k]])  {  ///当dp[v-W[k]]存在时才能够推导出dp[v]
                                dp[v]=true;
                                ans++;
                            }
                        }
                        k++;
                    }
                }
            }
            printf("%d
    ",ans);
    
        }
        return 0;
    }
    View Code

  • 相关阅读:
    粗看ES6之函数
    粗看ES6之变量
    https微信分享看不到图片的坑
    关于WebStorm,PhpStorm新版本输入中文问题
    ios下表单disabled样式重置
    关于IE的一些hack
    来自语文老师的教诲
    DP专题
    对近期参加的所有比赛的简略整理和好的idea的收集
    网络流学习
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5397054.html
Copyright © 2020-2023  润新知