poj3040 Allowance——贪心

Allowance
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.



这道题采用贪心的方法。先找大于c的直接加,当小于c时尽量用大面值的凑,然后凑到接近c但不超过时从最小的凑。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 10000000
struct money
{
    int va;//面值
    int zh;//该面值钱的数目
}A[25];
typedef struct money money;
bool cmp(money a,money b)
{
    return a.va>b.va;
}//按照面值从大到小来排序
int main()
{
    int n,c,t;
    int need[25];//用来储存第i种方案
    while(scanf("%d%d",&n,&c)!=EOF)
    {
        int sum=0;
        int i;
        int co=0;
        int lim=30;//lim的初始赋值应该值得注意,由于我是寻找面值从大到小,所以把lim的值应该比最大组数大,否则后面跳不出循环。如果面值从小到大寻找又不一样
        for(i=0;i<n;i++)
            scanf("%d%d",&A[i].va,&A[i].zh);
        sort(A,A+n,cmp);
        for(i=0;i<n;i++)
        {
            if(A[i].va>=c)
                sum=sum+A[i].zh;
            else
            {
                lim=i;//记录面值小于c的i组
                break;
            }
        }//大于c的直接加
        while(1)
        {
            memset(need,0,sizeof(need));
            t=c;//t是相当于一个c的替代品
            for(i=lim;i<n;i++)
            {
                if(!A[i].zh||!t)
                    continue;
                co=t/A[i].va;
                co=min(co,A[i].zh);
                need[i]=co;
                t=t-need[i]*A[i].va;
            }//面值小于c的尽量加大的,并且尽量接近或等于c而不超过c
            if(t)
            {
                for(i=n-1;i>=lim;i--)
                {
                    if(A[i].va>=t&&A[i].zh>need[i])
                    {
                        need[i]++;
                        t=0;
                        break;
                    }
                }
                if(t)
                    break;
            }//从面值最小的往上找,找到就跳出
            int minn=INF;
            for(i=lim;i<n;i++)
                if(need[i])
                    minn=min(minn,A[i].zh/need[i]);//第i种方案可以给多少个星期
            sum+=minn;
            for(i=lim;i<n;i++)
                if(need[i])
                    A[i].zh-=minn*need[i];//当第i种方案尽量用了张数后面值i所剩下的张数,进行i+1种方案
        }
        printf("%d\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值