usaco Mixing Milk

最优购买策略
本文介绍了一个简单的算法,用于解决如何在给定多个供应商及其价格的情况下,以最低的成本满足特定数量的需求。通过排序和贪心策略,确保了每次都能选择当前最经济的选项。

水题,水题,不多说。每次选择最便宜的就可以了。

/**
TASK: milk
ID: DickensTone
LANG: C++
**/
#include<cstdio>
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 5000 + 5;
struct node
{
    int cents;
    int tot;
    bool operator < (const node &a) const
    {
        return cents < a.cents;
    }
}pre[maxn];
int main()
{
    freopen("milk.in", "r", stdin);
    freopen("milk.out", "w", stdout);
    int n, m;
    while(scanf("%d%d", &n, &m) == 2)
    {
        int ans = 0;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d", &pre[i].cents, &pre[i].tot);
        }
        sort(pre, pre + m);
        for(int i = 0; i < m && n; i++)
        {
            int t = min(n, pre[i].tot);
            ans += t * pre[i].cents;
            n = n - t;
        }
        printf("%d\n", ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值