Cod sursa(job #2811327)

Utilizator AleXutzZuDavid Alex Robert AleXutzZu Data 1 decembrie 2021 20:53:28
Problema Lupul Urias si Rau Scor 36
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>

struct Sheep {
private:
    size_t wool = 0;
public:
    size_t distance = 0, max_step = 0;

    size_t getWool() const {
        return wool;
    }

    Sheep() = default;

    Sheep(std::istream &istream, size_t wolf_max_distance, size_t increment) {
        istream >> distance >> wool;
        max_step = (wolf_max_distance - distance) / increment + 1;
    }
};

inline bool operator<(const Sheep &_lhs, const Sheep &_rhs) {
    return _lhs.getWool() < _rhs.getWool();
}

int main() {
    std::ifstream input("lupu.in");
    std::ofstream output("lupu.out");

    std::priority_queue<Sheep, std::vector<Sheep>> woolQueue;
    size_t n, wolf_max_distance, increment, max_step = 0;
    std::vector<Sheep> sheep;
    input >> n >> wolf_max_distance >> increment;
    sheep.reserve(n);
    for (size_t i = 0; i < n; ++i) {
        sheep.emplace_back(input, wolf_max_distance, increment);
        if (sheep.back().max_step > max_step) max_step = sheep.back().max_step;
    }
    size_t total_wool = 0;
    for (size_t i = max_step; i >= 1; --i) {
        for (size_t j = 0; j < n; ++j) if (sheep[j].max_step == i) woolQueue.push(sheep[j]);

        if (!woolQueue.empty()) {
            total_wool += woolQueue.top().getWool();
            woolQueue.pop();
        }
    }
    output << total_wool;
    return 0;
}