HDU 3348 coins 最小化纸币数量贪心,和最大化纸币数量贪心

题意:你有1 5 10 50 ·100 (jiao)的纸币数量分别为a1 a2 a3 a4 a5

要去买一本价值为 P 的书,要求不用找钱(也就是你出的钱价值量正好是P)问使用最小的纸币数量,最多的纸币数量。


思路:一看就是贪心,求最小数量简单,但是求最大数量让我想了很久,第一次想要先最小化数量求出所应用的钱,然后对这些钱(用以小换大的方式去最大化数量),可是一直wrong。应该是这个贪心策略一直就不对。。。。逆向思维啊,就对了,想要求P价值对大数量的钱,也就是求(allValue - p)价值最小数量的钱。写一个Function就OK了。、

以下是代码!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
#define LOCAL
#define INF 0x3f3f3f3f
#define MAX_N 10000= 1;


int value[] = {1, 5, 10, 50, 100};
int amount[10];

int solveMin(int pt)
{
    int ans = 0;
    for(int i = 4; i >= 0; i--) {
        int amt = min(pt / value[i], amount[i]);
        pt -= value[i] * amt;
        ans += amt;
    }
    if(pt == 0)
        return ans;
    else
        return -1;
}

int main()
{
	#ifdef LOCAL
		freopen("b:\\data.in.txt", "r", stdin);
	#endif
    int T;
    scanf("%d", &T);

    while(T--) {
        int p;
        scanf("%d", &p);
        int allvalue = 0;
        int allamount = 0;
        for(int i = 0; i < 5; i++) {
            scanf("%d", &amount[i]);
            allvalue += amount[i] * value[i];
            allamount += amount[i];
        }
        int mi = solveMin(p);
        if(mi == -1)
            printf("-1 -1\n");
        else
        {
            int remain = allvalue - p;
            int ma = allamount - solveMin(remain);
            printf("%d %d\n", mi, ma);
        }
    }
    return 0;
}


Description

"Yakexi, this is the best age!" Dong MW works hard and get high pay, he has many 1 Jiao and 5 Jiao banknotes(纸币), some day he went to a bank and changes part of his money into 1 Yuan, 5 Yuan, 10 Yuan.(1 Yuan = 10 Jiao)
"Thanks to the best age, I can buy many things!" Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn't like to get the change, that is, he will give the bookseller exactly P Jiao.
 

Input

T(T<=100) in the first line, indicating the case number.
T lines with 6 integers each:
P a1 a5 a10 a50 a100
ai means number of i-Jiao banknotes.
All integers are smaller than 1000000.
 

Output

Two integers A,B for each case, A is the fewest number of banknotes to buy the book exactly, and B is the largest number to buy exactly.If Dong MW can't buy the book with no change, output "-1 -1".
 

Sample Input

    
    
3 33 6 6 6 6 6 10 10 10 10 10 10 11 0 1 20 20 20
 

Sample Output

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值