Submission #1197068

#TimeUsernameProblemLanguageResultExecution timeMemory
1197068chseee대표 선수 (KOI11_player)C++20
20 / 20
128 ms4504 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
using ll = long long;
// template end

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a[n + 5], ptr(n + 5, 0);
    priority_queue<pair<int, int>> pq;
    int mx = -1;
    int ans = 1e9 + 7;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            int dt;
            cin >> dt;
            a[i].push_back(dt);
        }
        sort(a[i].begin(), a[i].end());
        pq.push({-a[i][0], i});
        mx = max(mx, a[i][0]);
    }

    while (!pq.empty()) {
        int mn = -pq.top().first;
        int idx = pq.top().second;
        pq.pop();

        ans = min(ans, mx - mn);
        ptr[idx]++;
        if (ptr[idx] >= m) {
            break;
        }

        mx = max(mx, a[idx][ptr[idx]]);
        pq.push({-a[idx][ptr[idx]], idx});
    }
    cout << ans << "\n";
}

int main(void) {
    fastio;
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...