Editorial for 2195: Король ночі


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldb = long double;

#define pb emplace_back
#define forn(i, x, n) for (auto i = static_cast<int>(x); i < static_cast<int>(n); ++i)

#define int long long

struct init {
    init() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cout << fixed << setprecision(10);
    }

    ~init() {
        cerr << "Time elapsed: " << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s.\n";
    }
} init;

const int N = 100500 * 3, MOD = 1000 * 1000 * 1000 + 7;
const ldb EPS = 5e-12, HALF = 0.5;

struct man {
    int city;
    int id;
    int val;

    man(int a, int b, int c)
        : city(a), id(b), val(c) {}

    bool operator <(const man& other) const {
        return val < other.val;
    }
};

signed main() {
    int n, m;
    cin >> n >> m;

    int a[n + 10][m + 10];
    forn(i, 0, n) {
        forn(j, 0, m) {
            cin >> a[i][j];
        }
        sort(a[i], a[i] + m);
    }

    multiset<man> s;
    forn(i, 0, n)
        s.emplace(i, 0, a[i][0]);

    int ans = 1e9 + 1;
    multiset<man> best;

    while (true) {
        auto cur = *s.begin();
        auto last = *prev(s.end());

        int res = last.val - cur.val;
        if (res < ans) {
            ans = res;
            best = s;
        }

        if (cur.id == m - 1) {
            break;
        }

        s.erase(s.begin());
        s.emplace(cur.city, cur.id + 1, a[cur.city][cur.id + 1]);
    }

    for (auto q : best) {
        cout << q.val << ' ';
    }
}

Коментарі

Please read the guidelines before commenting.


Ще немає коментарів.