poj 3040 Allowance (贪心)

Allowance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 870 Accepted: 385

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.

Source



思路:

贪心,突破口:大v是小v的倍数。那么能用一个大v解决的肯定就不用几个小v了,因为小v可能有别的组合使解更优。

具体过程:v>c则直接加上b,然后按v从大到小排序,从大往小扫描,能加就加到尽量接近m,如果直接加到等于m那自然最好,如果没有则只需要从小到大扫描取一个就够了。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 25
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;

typedef long long ll;
int n,m,ans,cnt,mi;
int vis[maxn];
struct Node
{
    int v,b;
}pp[maxn];

bool cmp(Node xx,Node yy)
{
    return xx.v>yy.v;
}
void solve()
{
    int i,j,t,flag;
    while(1)
    {
        flag=1;
        t=0;
        memset(vis,0,sizeof(vis));
        for(i=1; i<=cnt; i++)
        {
            if(pp[i].b<=0) continue ;
            vis[i]=min(pp[i].b,(m-t)/pp[i].v);
            t=t+vis[i]*pp[i].v;
            if(t==m) break ;
        }
        if(t<m)
        {
            flag=0;
            for(i=cnt;i>=1;i--)
            {
                if(pp[i].b<=vis[i]) continue ;
                flag=1;
                vis[i]+=1;
                break ;
            }
        }
        if(!flag) break ;
        mi=INF;
        for(i=1;i<=cnt;i++)
        {
            if(vis[i])
            {
                mi=min(mi,pp[i].b/vis[i]);
            }
        }
        ans+=mi;
        for(i=1;i<=cnt;i++)
        {
            if(vis[i])
            {
                pp[i].b=pp[i].b-mi*vis[i];
            }
        }
    }
}
int main()
{
    int i,j,t,v,b;
    while(~scanf("%d%d",&n,&m))
    {
        cnt=ans=0;
        for(i=1; i<=n; i++)
        {
            scanf("%d%d",&v,&b);
            if(v>=m) ans+=b;
            else
            {
                pp[++cnt].v=v;
                pp[cnt].b=b;
            }
        }
        sort(pp+1,pp+cnt+1,cmp);
        solve();
        printf("%d\n",ans);
    }
    return 0;
}
/*
2 8
1 4
3 1
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值