zoukankan      html  css  js  c++  java
  • H

    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分别代表硬币的种类数和表的最大价值,接下来一行共2n个数,给你a1,a2,...an枚硬币,每一枚硬币都有各自的面值,然后有c1,c1...cn对应每一枚硬币的个数,让你求出我最多能付出多少钱

    分析:一开始我自己的想法是打算用贪心算法解决此问题的,我也认为是可行的,但是执行的时候卡住了,问题很久没解决,等我再思考一阵。

           然后就是看了别人的博客,很快就写出来了,此题是一个多背包问题,看了别人的模板后一下就写出来了,还不是很理解,恩,总的感觉各种背包问题都是有联系的,相差不大。

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int f[101000];
    int c[105];
    int num[105];
    int n,V;
    void ZeroOnePack(int cost,int weight)
    {
        for(int v=V;v>=cost;v--)
            f[v]=max(f[v],f[v-cost]+weight);
    }
    void CompletePack(int cost,int weight)
    {
        for(int v=cost;v<=V;v++)
            f[v]=max(f[v],f[v-cost]+weight);
    }
    void MultiplePack(int cost,int weight,int amount)
    {
        if(cost*amount>=V)
        {
            CompletePack(cost,weight);
            return;
        }
        int k=1;
        while(k<amount)
        {
            ZeroOnePack(k*cost,k*weight);
            amount=amount-k;
            k=k*2;
        }
        ZeroOnePack(amount*cost,amount*weight);
    }
    int main()
    {
        while(scanf("%d%d",&n,&V)&&n!=0&&V!=0)
        {
            for(int i=1;i<=n;i++)
                scanf("%d",&c[i]);
            for(int i=1;i<=n;i++)
                scanf("%d",&num[i]);
            memset(f,0,sizeof(f));
            for(int i=1;i<=n;i++)
                MultiplePack(c[i],c[i],num[i]);
            int ans=0;
            for(int i=1;i<=V;i++)
                if(f[i]==i)ans++;
            printf("%d
    ",ans);
        }
    }
    

     

     
     
     
  • 相关阅读:
    工作中简单又实用的vim配置
    宏定义的专业写法(工作中常用)
    17八皇后问题
    04文件与IO
    响应式布局编码
    静态布局,自适应布局,流体式布局,响应式布局概念
    CSS:<link>标签rel和media的解释
    我想去的公司的入职要求
    JS:引用类型,基本类型
    Android Launcher 详解
  • 原文地址:https://www.cnblogs.com/lbyj/p/5750878.html
Copyright © 2011-2022 走看看