POJ 1742 Coins

一、题目描述

1、英文

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.

2、简单翻译

你有n种硬币,第"i"(1<=i<=n)种硬币的面值为"Ai",数量为"Ci",求出这些硬币能组成的面值(1<=面值<=m)种类数。

3、问题约束

Time Limit: 3000MSMemory Limit: 30000K
Total Submissions: 57062Accepted: 18802

(1<=n<=100),(1<=Ai<=100000,1<=Ci<=1000),(m<=100000)

4、样例输入

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

5、样例输出

8
4

二、解题

1、解题思路

我们可以用定义dp数组,代表组成数字j之后,剩余的第i种金币的数量,即如下递推关系

//定义规则为,dp[i+1][j]代表前i种金币,表示出面值为j后,第i种金币剩余的数量。
//则,如果dp[i][j]>=0,则代表前i-1种金币,可以表示出j,则第i种金币全部剩余,那么第i种金币剩余 coint[i]个
if(dp[i][j]>=0){
    dp[i+1][j]=count[i];
}
//如果j>=amount[i]&&dp[i+1][j-amount[i]]>0,则代表用第i种金币,可以组成j-amount[i],并且有金币剩余
//那么额外拿出一枚剩余的金币,即可组成j,(j>=amount[i],提前判断防止数组越界)
else if(j>=amount[i]&&dp[i+1][j-amount[i]]>0){
    dp[i+1][j]=dp[i+1][j-amount[i]]-1;
}
//否则,则无法表示j,那么设置为-1,代表表示不了
else{
    dp[i][j]=-1;
}

那么再考虑当m<=0,且n>0的情况,则[1,m]区间不存在,那么输出0即可。

当n等于0,并且m等于0,就跳出循环,则已经可以写代码了。

三、代码

while(true){
    scanf("%d%d",&n,&m);
    if(n>0&&m>0){
        for(int i=0;i<n;i++){
            scanf("%d",&amount[i]);
        }
        for(int i=0;i<n;i++){
            scanf("%d",&amount[i]);
        }
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j]=-1;
            }
        }
        dp[0][0]=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<=m;j++){
                if(dp[i][j]>=0){
                    dp[i+1][j]=count[i];
                }else if(j>=amount[i]&&dp[i+1][j-amount[i]]>0){
                    dp[i+1][j]=dp[i+1][j-amount[i]]-1;
                }else{
                    dp[i+1][j]=-1;
                }
            }
        }
    }else if(n>0&&m<=0){
        for(int i=0;i<n;i++){
            scanf("%d",&amount[i]);
        }
        for(int i=0;i<n;i++){
            scanf("%d",&amount[i]);
        }
        printf("0\n");
    }else{
        break;
    }
}

由于数据规模比较大,因此我只开了一个2行的数组,然后利用mod2反复调用,同时,对于amount和count我利用了pair数据结构,first代表amount,second代表count,AC代码如下。

#include <iostream>
using namespace std;
//dp[i][j]代表组成j之后,第i种金币剩余的数量,不能组成,则dp[i][j]=-1 
typedef pair<int,int> P;
int dp[2][100009];
int n,m;
P coins[109];
int main(){
    while(true){
        scanf("%d%d",&n,&m);
        if(n>0&&m>0){
            for(int i=0;i<n;i++){
                scanf("%d",&coins[i].first);
            }
            for(int i=0;i<n;i++){
                scanf("%d",&coins[i].second);
            }
            for(int i=0;i<2;i++){
                for(int j=0;j<=m;j++){
                    dp[i][j]=-1;
                }
            }           
            dp[0][0]=0; 
            for(int i=0;i<n;i++){
                for(int j=0;j<=m;j++){
                    if(dp[(i)%2][j]>=0){
                        dp[(i+1)%2][j]=coins[i].second;
                    }else if(j>=coins[i].first&&dp[(i+1)%2][j-coins[i].first]>0){
                        dp[(i+1)%2][j]=dp[(i+1)%2][j-coins[i].first]-1;
                    }else{
                        dp[(i+1)%2][j]=-1;
                    }
                }
            }
            int ans=0;
            for(int i=1;i<=m;i++){
                if(dp[n%2][i]>=0){
                    ans++;
                }
            }
            printf("%d\n",ans);
        }else if(n>0&&m<=0){
            for(int i=0;i<n;i++){
                scanf("%d",&coins[i].first);
            }
            for(int i=0;i<n;i++){
                scanf("%d",&coins[i].second);
            }
            printf("0\n");
        }else{
            break;
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值