poj 3040

       题意:Join有n中面额不同的硬币(注意,面额按从小到大是成倍数增加的,这也是本题能贪心的前提条件),他每个星期都要发不低于C元钱的工资,现在给出n种硬币的面额和数量,要你求出join最多能发多少个星期的工资?

      分析:一开始贪的不对,后来在网上看了一些题解后才A了,但感觉贪心的思路还是有些问题,但有一点是明确的,在凑齐面额时,其大小即要大于等于C,又要尽量接近C,这样才能使每个月的损失的金额尽量少。

                 (1)面额不小于C的直接计数,这点很简单;

                 (2)对那些面额小于C的,首先进行排序,然后从大到小扫描一遍,扫描时,每遇到一个硬币都尽量拿,只要不超过C就行,拿不了了就看下一个。

                 (3)若执行完第二步后任小于C,就从小到大再扫描一遍,以凑齐C。

代码如下:

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-5;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const double M=1e-8;
const int N=1e4+10;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int n,m,s[N];
struct node
{
    int x,y;
};
bool cmp(const node a,const node b)
{
    return a.x==b.x?a.y<b.y:a.x<b.x;
}
node a[N];
int slove(int len,int ans)
{
    int i,j,use[22];
    while(1) {
        int sum=m,mn;
        MS(use,0);
        for (i=len-1;i>=0;i--) if (a[i].y) {
            mn=min(sum/a[i].x,a[i].y);
            sum-=mn*a[i].x;
            use[i]=mn;
        }
        if (sum>0) {
            for (i=0;i<len;i++) if (use[i]<a[i].y && a[i].x>=sum) {
                 use[i]++;
                 sum-=a[i].x;
                 break;
            }
        }
        mn=INF;
        if (sum>0) break;  // haven't money enough
        for (i=0;i<len;i++) if (use[i]) {
            mn=min(mn,a[i].y/use[i]); //   <span style="font-family: Arial, Helvetica, sans-serif;">use[i] 表示为了凑齐C而与其它硬币组合时,第i种硬币所需数量</span>
        }
        ans+=mn;
        for (i=0;i<len;i++) if (use[i]) {
            a[i].y-=use[i]*mn;
        }
    }
    return ans;
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for (i=0,j=0;i<n;i++) {
            int x,y;
            scanf("%d%d",&x,&y);
            if (x<m) {
                a[j].x=x;
                a[j++].y=y;
            } else {  ans+=y; }
        }
        sort(a,a+j,cmp);
        cout<<slove(j,ans)<<endl;
    }
}


/*
5 9
1 10
2 20
4 30
8 40
16 50

*/






















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值