题解 | 小熊吃糖

#include <iostream>
#include <type_traits>
#include <vector>
#include <algorithm>
using namespace std;

struct Bear {
    int fight = 0;
    int hungry = 0;
    int id = 0;
    int left_hungry = 0;
    Bear() = default;
};

int main() {
    int n;
    cin >> n;
    int m;
    cin >> m;
    std::vector<int> candy_energy(m, 0);
    for (int i = 0; i < m; i++) {
        cin >> candy_energy[i];
    }
    std::vector<Bear> bears(n);
    for (int i = 0; i < n; i++) {
        cin >> bears[i].fight >> bears[i].hungry;
        bears[i].id = i;
        bears[i].left_hungry = bears[i].hungry;
    }
    std::sort(bears.begin(), bears.end(),
    [&](const Bear & b1, const Bear & b2) {
        return b1.fight < b2.fight;
    } );

    std::sort(candy_energy.begin(), candy_energy.end());
    std::vector<bool> used_candy_energy(m, false);
    for (int i = bears.size() - 1; i >= 0; i--) {
        // bears[i]
        for (int j = candy_energy.size() - 1; j >= 0; j--) {
            if (bears[i].left_hungry == 0) {
                break;
            }
            if (bears[i].left_hungry >= candy_energy[j]) {
                if (used_candy_energy[j] == false) {
                    used_candy_energy[j] = true;
                    bears[i].left_hungry =
                        bears[i].left_hungry - candy_energy[j];
                }
            }
        }
    }
    std::sort(bears.begin(), bears.end(),
    [&](const Bear & b1, const Bear & b2) {
        return b1.id < b2.id;
    });

    for (const auto& bear : bears) {
        cout << bear.left_hungry << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务