POJ 3040 - Allowance(贪心)

Description

As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins).Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week.Please help him ompute the maximum number of weeks he can pay Bessie.

Input

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.

Output

* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance

Sample Input

3 6
10 1
1 100
5 120

Sample Output

111
------------------------------------------------

思路:

贪心三个步骤:

1:面额大于等于c的硬币,无法节约,全部先发掉。

2:然后对硬币面额从大到小尽量凑得接近C,允许等于或不足C,但是不能超出C

3:接着按硬币面额从小到大凑满C(凑满的意思是允许超出一个最小面值,ps此处的最小面值指的是硬币剩余量不为0的那些硬币中的最小面值),凑满之后得出了最优解,发掉。再进入步骤2,直到剩下的钱数无法凑满c。

CODE:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
struct node {
    int val, num;
    bool operator < (const node &a) const{
        return val > a.val;
    }
}coin[25];
int need[25];

int main()
{
//freopen("in", "r", stdin);
    int n, c;
    while(~scanf("%d %d", &n, &c)) {

        int ans = 0, nj = 0;
        for(int i = 0; i < n; ++i) {
            int v, b;
            scanf("%d %d", &v, &b);
            if(v >= c) ans += b;
            else {
                coin[nj].val = v; coin[nj++].num = b;
            }
        }
        sort(coin, coin + nj);
        while(1) {
            int res = c;
            memset(need, 0, sizeof(need));
            for(int i = 0; i < nj; ++i) {
                if(res && coin[i].num) {
                    int c = res / coin[i].val;
                    c = min(c, coin[i].num);
                    need[i] = c;
                    res -= c*coin[i].val;
                }
            }
            if(res) {
                for(int i = nj-1; i >= 0; --i) {
                    if(coin[i].val >= res && (coin[i].num - need[i])) {
                        need[i]++;
                        res = 0;
                        break;
                    }
                }
                if(res) break;
            }
            int d = 10000000;
            for(int i = 0; i < nj; ++i) {
                if(need[i]) {
                    d = min(d, coin[i].num/need[i]);
                }
            }
            ans += d;
            for(int i = 0; i < nj; ++i) {
                coin[i].num -= d*need[i];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值