poj3040 - Allowance - 贪心(好题)

Allowance

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7721 Accepted: 3011

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

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.

思路:

这道题体现了贪心的思想,(不搜题解,本菜菜是想不到的...)

1、若价值>=c,则ans直接加上其有的数量

剩下都是面值小于c的小钱币,我们要把其凑成c,凑的方法:

2、先从大面值的凑,比如:c=65

val:   3   5    7

num:12  5   3

先从7凑,3*7=21,然后再从5凑5*5=25,c=65-21-25=19

最后凑3 ,19/3=6,c=19-6*3=1

3、此时,c还剩下1没凑到,则从小往大凑,1个3就行了

这里的思想,我感jio是因为,先要从大面值的凑,(类似先把大石头放入空杯子)

放下后,发现杯子还有空隙,则从小面值的凑(类似把沙子撒进杯子)

这样就是用的很“节省”,最后用一个3来凑剩下的1,总比用一个5或一个7来凑要好

代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
const int N=30,mod=1e9+7;
const int INF=0x3f3f3f3f;
struct A{
    int val,num;
}coin[N];
int use[N];
bool cmp(A a,A b){
    return a.val<b.val;
}

int main(){
    int n,c;
    scanf("%d%d",&n,&c);
    for(int i=0;i<n;i++){
        scanf("%d%d",&coin[i].val,&coin[i].num);
    }
    sort(coin,coin+n,cmp);
    int ans=0;
    for(int i=0;i<n;i++){
        if(coin[i].val>=c){
            ans+=coin[i].num;
            coin[i].num=0;
        }
    }
    while(1){//要凑够c
        int now=c;
        for(int i=n-1;i>=0;i--){//先从大到小凑
            if(coin[i].num>0){
                int m=now/coin[i].val;//判断凑m个coin[i]到now
                m=min(m,coin[i].num);
                use[i]=m;
                now-=m*coin[i].val;
            }
            if(now==0)break;
        }

        if(now>0){//从小凑
            for(int i=0;i<n;i++){
                while(coin[i].num>use[i]){
                    use[i]++;
                    now-=coin[i].val;
                    if(now<=0)break;
                }
                if(now<=0)break;
            }
        }
        if(now>0)break;//凑不够一个c了,所以答案不变
        int day=INF;
        for(int i=0;i<n;i++){//每个硬币用了use[i]后终于凑成了一个c
            if(use[i]){//每个硬币能凑成a[i].num/use[i]个c
                day=min(day,coin[i].num/use[i]);//最多能凑成day个c
            }
        }
        ans+=day;
        for(int i=0;i<n;i++){//更新每个硬币的数量
            if(use[i]){
                coin[i].num-=day*use[i];
                use[i]=0;
            }
        }
    }
    printf("%d\n",ans);
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值