hust 1254 && hust 1510 Exact Change

题目描述

• Seller: That will be fourteen dollars. • Buyer: Here's a twenty. • Seller: Sorry, I don't have any change. • Buyer: OK, here's a ten and a five. Keep the change. When travelling to remote locations, it is often helpful to bring cash, in case you want to buy something from someone who does not accept credit or debit cards. It is also helpful to bring a variety of denominations in case the seller does not have change. Even so, you may not have the exact amount, and will have to pay a little bit more than full price. The same problem can arise even in urban locations, for example with vending machines that do not return change. Of course, you would like to minimize the amount you pay (though you must pay at least as much as the value of the item). Moreover, while paying the minimum amount, you would like to minimize the number of coins or bills that you pay out.

输入

The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer, the price in cents of the item you would like to buy. The price will not exceed 10 000 cents (i.e., $100). The following line contains a single integer n, the number of bills and coins that you have. The number n is at most 100. The following n lines each contain one integer, the value in cents of each bill or coin that you have. Note that the denominations can be any number of cents; they are not limited to the values of coins and bills that we usually use in Canada. However, no bill or coin will have a value greater than 10 000 cents ($100). The total value of your bills and coins will always be equal to or greater than the price of the item.输出

For each test case, output a single line containing two integers: the total amount paid (in cents), and the total number of coins and bills used.

样例输入

1
1400
3
500
1000
2000

样例输出

1500 2
从数据规模一看,0-1背包搞定,不过题目要求是只能多付,不能少付,一开始还以为可以少付,WA了,
#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

const int maxn=200;

int u,num[20001],a[101];

int main()
{
    int t,n,mon;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&u);
        scanf("%d",&n);
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        for (int i=1;i<=20000;i++) num[i]=maxn;
        num[0]=0;
        for (int i=1;i<=n;i++)
        for (int j=20000;j>=a[i];j--)
        num[j]=min(num[j-a[i]]+1,num[j]);
        for (int i=u;i<=20000;i++)
        if (num[i]!=maxn) {mon=i;break;}
        /*for (int i=u;i>=0;i--)
        {
            if (u-i<mon-u && num[i]!=maxn) {mon=i;break;}
            else if(num[i]!=maxn && u-i==mon-u && num[i]<num[mon])
            {
                mon=i;break;
            }
            else if(u-i>mon-u) break;
        }*/
        printf("%d %d\n",mon,num[mon]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/chensunrise/p/3723244.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值