7.19 Allowance(贪心,*******思路理顺)

Allowance

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种面值v1,v2……的钱币,分别有b1,b2……张,每周要选出总和不少于c的钱币,问最多能坚持多少周,其中大的钱币一定是小钱币的倍数

输入:

n c
接下来n行: vi bi

输出:

最大的周数

思路:

先排除面值大于c的纸币
从大往下选纸币,尽可能补满c,补不满的再拿稍微大一点的,使超过c
具体见代码注释

注意:

怎么优化缩短运行时间


#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
struct coin{
    LL v,n;
    coin(LL v=0,LL n=0):v(v),n(n) {};
};
bool operator < (const coin& a, const coin& b){
    return a.v>b.v;
}
coin c[22];
int mark[22];
LL n,cost;


int main()
{
    while(~scanf("%lld%lld",&n,&cost)){
        int s,t,cnt=0;
        for(int i=0;i<n;i++){
             scanf("%lld%lld",&c[i].v,&c[i].n);
        }
        sort(c,c+n);   //按面值从大到小排序
        for(int i=0;i<n;i++)
        if(c[i].v>=cost){
            cnt+=c[i].n; c[i].n=0;
        } else break;    //把面值本身大于cost的纸币排除

        while(1){        //循环,每找到一种最优的取币方法即退出循环
            LL v=cost;
            LL minn=1e9+1e5;
            memset(mark,0,sizeof(mark));
            while(v>0){
                for(s=0;s<n;s++)
                    if(c[s].n>mark[s]&&c[s].v<=v&&c[s].n) break; //找v以内能取到的最大面值的纸币
                if(s==n)
                  {
                    for(s=n-1;s>=0;s--)
                        if(c[s].n>mark[s]&&c[s].n&&c[s].v>=v) break;  //找能补满v,最小面值的纸币
                    if(s==-1) goto loop;  //都不存在,则v无法补满,退出这组数据
                  }
                  if(v>c[s].v) {
                            mark[s]=min(v/c[s].v,c[s].n);  //需要多少个s币,能最大的补满v  (注意,不能超过s币现有的张数)
                        }
                    else mark[s]++;  //若加入这张纸币总和大于cost,则只需要一张
                if(minn>c[s].n/mark[s])
                    minn=c[s].n/mark[s];  //minn记录这种纸币组合最多可以有多少组
                v-=c[s].v*mark[s]; //刷新需要补的v值
            }
            cnt+=minn;  //加上可以去掉的此种组合数
            for(int i=0;i<n;i++)
                if(mark[i]){
                    c[i].n-=minn*mark[i];  //刷新每张纸币的个数
                }
        }
        loop:
            printf("%d\n",cnt);   //终于输出解……

    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值