zoukankan      html  css  js  c++  java
  • 洛谷 P2095 【营养膳食】

    P2095 【营养膳食】

    这道题是用贪心的思路的,因为每份食物的重量都是1,所以不存在动归的问题。

    那要怎么贪呢,就是先吃脂肪最多的那份食物

    下面上代码

    #include <bits/stdc++.h>
    
    using namespace std;
    int n, m, k;
    int eatub[105];//eat upper bound 第i种食物最多能吃的数量
    struct food_type {
        int fat;//脂肪
        int type;//所属的种类
    } food[205];
    
    bool judge(food_type &a, food_type &b) {
        return a.fat > b.fat;//按脂肪降序排列
    }
    
    int main() {
    
        //读入
        int n, m, k;
        int ans = 0;
        cin >> n >> m >> k;
        for (int i = 0; i < k; ++i)
            cin >> eatub[i];
        for (int i = 0; i < n; ++i)
            cin >> food[i].fat >> food[i].type;
        //贪心的思路,先吃脂肪多的
        //排序
        sort(food, food + n, judge);
        //贪心
        for (int i = 0; i < n; ++i) {//最好情况就是把所有食物吃光
            if (m > 0 && eatub[food[i].type - 1] > 0) {//如果还没饱并且这种食物还能吃
                //把它吃掉
                eatub[food[i].type - 1]--;
                m--;
                ans += food[i].fat;
            }
        }
        cout << ans;
        return 0;
    }
  • 相关阅读:
    【转载】Linux 内核启动时间分析
    hackbench
    c用户组函数
    c环境变量操作函数
    c网络接口套接字函数
    c信号处理函数
    c进程操作函数
    c文件内容操作函数
    c文件操作
    c数据结构和算法
  • 原文地址:https://www.cnblogs.com/Iuppiter/p/12207795.html
Copyright © 2011-2022 走看看