Allowance

</pre></div></div></div></div><div style="text-align:center; margin-top:30px; clear:both"><div style="text-align:left"><span style="font-family: Simsun; font-size: 24px;"></span><div id="contest_tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all" style="position: relative; padding: 0.2em; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(170, 170, 170); color: rgb(34, 34, 34); border-radius: 4px; min-width: 0px; background: transparent;"><div id="problem" aria-labelledby="ui-id-2" class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel" aria-hidden="false" style="border: 0px solid rgb(170, 170, 170); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; padding: 1em 1.4em; font-size: 14px; background: 0px 50%;"><div class="plm" style="text-align: center; font-family: Arial, Helvetica, sans-serif; font-size: 12pt; clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;">Allowance</div><span id="crawlSuccess" class="crawlInfo"><strong>Time Limit:</strong><span id="timeLimit">1000</span>MS     <strong>Memory Limit:</strong><span id="memoryLimit">65536</span>KB     <strong>64bit IO Format:</strong><span id="_64IOFormat">%lld & %llu</span></span><div id="problem_opt" style="font-size: 12px; margin-top: 10px;"><a target=_blank id="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank id="problem_status" href="http://acm.hust.edu.cn/vjudge/contest/123357#status//K/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Status</span></a> <a target=_blank id="problem_practice" target="_blank" href="http://acm.hust.edu.cn/vjudge/problem/27577" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Practice</span></a> <a target=_blank id="problem_origin" target="_blank" href="http://acm.hust.edu.cn/vjudge/problem/27577/origin" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">POJ 3040</span></a></div></div><div style="width: 960px; margin: auto;"><div id="desc_K_0" class="desc_template"><div class="vj_description"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-size: 12pt;">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.</div></div></div><div class="vj_input"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-size: 12pt;">* 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.</div></div></div><div class="vj_output"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Output</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-size: 12pt;">* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance</div></div></div><div class="vj_sampleInput"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Sample Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><pre class="sio" style="white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New', Courier, monospace; font-size: 12pt;">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元,约翰有n种面值的钱币,第i种的面值为v_i,数量有b_i。问这些钱最多给贝西发多少天的工资。注意,每种面值的金钱都是下一种的面值的倍数。

这一题分三步解决:

     1. 按照面值从大到小取,面值大于等于C的,直接取光。

     2. 再按面值从大到小取,凑近C,可以小于等于C,但不能大于C。
    
     3.最后从小到大取,凑满C,这里的凑满可以等于大于C。然后将上述2,3步取到的面值全部取走,再转入步骤2,这样每次找到的取法就是当前最优取法,直到所剩下的金币总价值不够C结束。
代码看懂了又加了不少注释方便以后看懂 
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#define INF 0x3f3f3f   
using namespace std;  
int use[30];//记录当前取法的第i种面值取的个数   
struct node  
{  
    int v,b;  
}a[25];  
  
int cmp(node a,node b)  
{  
    return a.v<b.v;  
}   
  
int main()  
{  
    int n,c,i,cnt,ans,k,m;  
    while(scanf("%d%d",&n,&c)!=EOF)  
    {  
        for(i=0;i<n;++i)  
            scanf("%d%d",&a[i].v,&a[i].b);  
        sort(a,a+n,cmp);  
        ans=0;  
        for(i=n-1;i>=0;i--)//第一步,满足大于C的面值全部取走   
        {  
            if(a[i].v>=c)  
            {  
                ans+=a[i].b;  
                a[i].b=0;  
            }  
        }  
        while(1)//每次循环都在找一次当前最优取法,直到剩下的总金额小于C元   
        {  
            int sign=0;  
            cnt=c;  
            memset(use,0,sizeof(use));  
            for(i=n-1;i>=0;--i)//第二步,从大到小取,不能超过C的值   
            {  
                if(a[i].b)  
                {  
                    k=cnt/a[i].v;  //这个面额的零钱能不能填补所缺的工资 
                    m=min(k,a[i].b);   
                    cnt-=m*a[i].v;  //凑到的工资 
                    use[i]=m;
                    if(cnt==0)  //已经凑齐一周的工资 
                    {  
                        sign=1;  
                        break;  
                    }  
                    
                }  
            }  
            if(cnt>0)  //不可能为负 
            {  
                for(i=0;i<n;++i)//第三步,从小到大取,凑满C   
                {  
                    if(a[i].b>use[i])   
                    {  
                        while(use[i]<a[i].b)  //最小的那个全部用完为止 
                        {  
                            cnt-=a[i].v;  
                            use[i]++;  //多用一份这个面额的零钱 
                            if(cnt<=0)  //凑齐工资 
                            {  
                                sign=1;  
                                break;  
                            }  
                        }  
                    }  
                    if(sign)  
                        break;  
                }  
            }  
            if(!sign)  
                break;  
            m=INF;  
            for(i=0;i<n;++i)  
            {  
                if(use[i])//找到当前取法的能取的总次数   
                    m=min(m,a[i].b/use[i]);  //只能取最少份额的零钱 
            }  
            ans+=m;  
            for(i=0;i<n;++i)  
            {  
                if(use[i])  
                    a[i].b-=m*use[i]; //减去那个最小份额额零钱 
            }   
        }  
        printf("%d\n",ans);  
    }  
    return 0;  
} 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值