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.

解题报告:
很无聊的题,贪心一下就好,原则是选择能超过C的最小搭配,那么显然对于本身价值就大于C的直接用掉就好,如果小于的话我们选择最小搭配
考虑实现:
我们要先用掉面额比较大的,拿它去搭配较小面额,所以我们能用多少就用多少,对于剩余的我们就用小面额的补上,然后找到循环节,一次性减掉即可,这样一次至少用掉一种面额,所以复杂度\(O(N^2)\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=25;
struct node{
    int v,w;
    bool operator <(const node &pp)const{
        return v<pp.v;
    }
}a[N];
ll ans=0;int r[N];
void work()
{
    int n,c,lim=1e6;
    scanf("%d%d",&n,&c);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].v,&a[i].w);
        if(a[i].v>=c){
            ans+=a[i].w;
            a[i].w=0;
        }
    }
    sort(a+1,a+n+1);
    int tot=0,tmp;
    while(tot<=0){
        tot=c;
        for(int i=n;i>=1;i--){
            tmp=Min(a[i].w,tot/a[i].v);
            tot-=tmp*a[i].v;
            r[i]=tmp;
        }
        if(tot){
            for(int i=1;i<=n;i++){
                if(a[i].w>r[i])tot-=a[i].v,r[i]++;
                if(tot<=0)break;
            }
        }
        if(tot>0)break;
        tmp=lim;
        for(int i=1;i<=n;i++)
            if(r[i]>0)tmp=Min(tmp,a[i].w/r[i]);
        for(int i=1;i<=n;i++){
            if(r[i]>0)a[i].w-=tmp*r[i];
            r[i]=0;
        }
        ans+=tmp;
    }
    printf("%lld\n",ans);
}

int main()
{
    work();
    return 0;
}

转载于:https://www.cnblogs.com/Yuzao/p/7499208.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值