SDAU 课程练习3 1020

Problem T

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 2
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.<br><br>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
 
题目大意:


现在有好多硬币,面值分别为   a1.a2....ai,给出每个硬币的数量,求可以组成多少个不同的价值。

思路:

一开始的思路还是将相同的物品转化成多件物品,然后进行  0  1  求解,不出意料的超时。后来继续看了看背包九讲,明白了这个是多重背包的问题。
关于多重背包求解,具体的思路是这样。

当这件物品的数量*这件物品的体积  >=    背包容量   那么这件这就相当于完全背包,完全背包的定义:每件物品无限可用。
如果不够的话,那么就用  0  1  来解,把  1,2,3...k  件物品当成一件物品来看。

然后就这样喽。

感想:

。。。。。。。什么什么的好麻烦。最近心态不行。很无奈。

AC代码:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
int val[900005];
int num[900005];
int dp[900005];
int v;
void ZeroOnePack(int cost,int weight)
{
    for(int i=v;i>=cost;i--)
       if(dp[i-cost]+weight>dp[i]) dp[i]=dp[i-cost]+weight;
}
void CompletePack(int cost,int weight)
{
    for(int i=cost;i<=v;i++)
        if(dp[i-cost]+weight>dp[i]) dp[i]=dp[i-cost]+weight;
}
void MultiplePack(int cost ,int weight,int amount)
{
    if(cost*amount>=v) CompletePack(cost,weight);
    else
    {
        for(int k=1;k<amount;)
        {
            ZeroOnePack(k*cost,k*weight);
            amount-=k;
            k<<=1;
        }
        ZeroOnePack(amount*cost,amount*weight);
    }
}
int main()
{
    //freopen("r.txt","r",stdin);
    int i,n,m,j;

    while(~scanf("%d%d",&n,&v))
    {
        if(n==0&&v==0) break;

        for(i=0;i<n;i++)
        {
            scanf("%d",&val[i]);
        }
        for(i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
        }

        memset(dp,0,sizeof(dp));

        for(i=0;i<n;i++)
        {
            MultiplePack(val[i],val[i],num[i]);
        }
        int temp=0,cou=0;
        for(i=0;i<=v;i++)
        {
            if(temp!=dp[i])
            {
                temp=dp[i];
                cou++;
            }
        }
        cout<<cou<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值