[挑战程序设计竞赛] POJ 3040 - Allowance

题意:

给定N,C分别代表面值的种类 和 每周至少要发的钱数。

接着输入N行面值 和 对应面值的个数。

问给定这些钱最多能发多少周?

思路:

1、当对应面值 >= C时,不需要贪心,直接计算即可。

2、当对应面值 < C时,按面值从大到小贪心。贪心的过程不能浪费任何面值。

3、第二步结束后,显然能不浪费的面值都已经拿完了,如果第二步贪心的结果val < C,那必然要浪费一个最小能满足val + x > C的面值。

重复上述步骤即可。。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
//#define _Test
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int main()
{
    #ifdef _Test
        freopen("test.in", "r", stdin);
        freopen("test.out", "w", stdout);
        srand(time(NULL));
    #endif
    int N, C, ans;
    pair<int, int> cost[20];
    while(~scanf("%d %d", &N, &C))
    {
        for(int i = 0; i < N; i++)
        {
            scanf("%d %d", &cost[i].first, &cost[i].second);
        }
        ans = 0;
        sort(cost, cost+N);
        for(int i = 0; i < N; i++)
        {
            if(cost[i].first >= C)
            {
                ans += cost[i].first / C * cost[i].second;
                cost[i].second = 0;
            }
        }
        while(true)
        {
            int val = C;
            int sum = 0;
            for(int i = N - 1; i >= 0; i--)
            {
                if(val && cost[i].second)
                {
                    int k = min(val / cost[i].first, cost[i].second);
                    if(k)
                    {
                        val -= cost[i].first * k;
                        cost[i].second -= k;
                        sum += cost[i].first * k;
                    }
                }
            }
            for(int i = 0; i < N; i++)
            {
                if(val && cost[i].second && cost[i].first > val)
                {
                    val = 0;
                    sum += cost[i].first;
                    cost[i].second--;
                    break;
                }
            }
            if(val > 0) break;
            ans += sum / C;
        }
        printf("%d\n", ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值