Submission #524131

#TimeUsernameProblemLanguageResultExecution timeMemory
524131FireGhost1301The Kingdom of JOIOI (JOI17_joioi)C++11
100 / 100
904 ms17928 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 2000 + 3;
int n, m, a[N][N];
int mi = INT_MAX, mx = 0;

bool chk(int d) {
    for (int i = 1, p = 0; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (a[i][j] + d < mx) p = max(p, j + 1);
        }
        for (int j = 1; j <= m; ++j) {
            if (a[i][j] - d > mi && j < p) return 0;
        }
    }
    return 1;
}

int calc() {
    int l = 0, r = mx - mi - 1, ans = r + 1, mid;
    while (l <= r) {
        mid = (l + r) >> 1;
        if (chk(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    return ans;
}

void solve() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> a[i][j];
            mx = max(mx, a[i][j]);
            mi = min(mi, a[i][j]);
        }
    }
    int ans = calc();
    reverse(a + 1, a + n + 1);
    ans = min(ans, calc());
    for (int i = 1; i <= n; ++i)
        reverse(a[i] + 1, a[i] + m + 1);
    ans = min(ans, calc());
    reverse(a + 1, a + n + 1);
    ans = min(ans, calc());
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...