Allowance POJ - 3040

Allowance POJ - 3040

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

Hint

INPUT DETAILS:

FJ would like to pay Bessie 6 cents per week. He has 100 1-cent coins,120 5-cent coins, and 1 10-cent coin.

OUTPUT DETAILS:

FJ can overpay Bessie with the one 10-cent coin for 1 week, then pay Bessie two 5-cent coins for 10 weeks and then pay Bessie one 1-cent coin and one 5-cent coin for 100 weeks.

题意:
给牛津贴,有N货币,每个星期至少给牛C元,货币不可以拆开(比如,要求给6元,但是只有两个5元,那么就给这头牛10元)。
问:做多能给牛几个星期?

题解:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std

int N, C;
struct node{
    int d, c;
};
node a[25];

bool cmp(const node & a, const node & b){
    return a.d > b.d;
}

int main(){
    cin >> N >> C;
    for (int i = 0; i < N; i++){
        scanf("%d %d", &a[i].d, &a[i].c);
    }
    sort(a, a + N, cmp);
    int cnt = 0;
    int j = 0;
    //j记录a[j].d >= C 的下标
    for (; j < N; j++){
        if (a[j].d < C)
            break;
    }
    //用那些货币面值大于C的 能撑多长时间
    for (int i = 0; i < j; i++){
        cnt += (a[i].d / C) * a[i].c;
    }
    while (true){
        int now = 0;
        //剩下的货币中,显然最大的和最小的拼凑起来 减去C的 值最小, 即浪费的最少
        //找面值大的(可能使用超过一张,比如C=7,有两张1元,两张3元)
        for (int i = j; i < N; i++){
            while (a[i].c && now + a[i].d <= C){
                now += a[i].d;
                a[i].c--;
            }
        }
        //从数列尾端找面值小的
        for (int i = N - 1; i >= j; i--){
            while (a[i].c && now < C){
                now += a[i].d;
                a[i].c--;
            }
        }
        if (now < C)
            break;
        cnt++;
    }
    cout << cnt << endl;
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值