| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1277769 | wedonttalkanymore | The Kingdom of JOIOI (JOI17_joioi) | C++20 | 675 ms | 63672 KiB | 
#include <bits/stdc++.h>
/*
    Brute to win
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e3 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int h, w, a[N][N], b[N][N];
int minn = inf;
void rolate() {
    // for (int i = 1; i <= h; i++) {
    //     for (int j = 1; j <= w; j++) {
    //         cout << a[i][j] << ' ';
    //     }
    //     cout << '\n';
    // }
    // cout << a[1][4] << '\n';
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            b[j][h - i + 1] = a[i][j];
            // cout << i << ' ' << j << ' ' << w - j + 1 << ' ' << a[i][w - j + 1] << '\n';
        }
    }
    swap(h, w);
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            a[i][j] = b[i][j];
        }
    }
}
bool check(int mid) {
    int vmax = 0, vmin = inf, last = w;
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= last; j++) {
            if (a[i][j] - minn > mid) {
                last = j - 1;
                break;
            }
        }
        for (int j = last + 1; j <= w; j++) {
            vmin = min(vmin, a[i][j]);
            vmax = max(vmax, a[i][j]);
        }
    }
    return vmax - vmin <= mid;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    cin >> h >> w;
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            cin >> a[i][j];
            minn = min(minn, a[i][j]);
        }
    }
    int ans = inf;
    for (int i = 1; i <= 4; i++) {
        int l = 1, r = 1e9, res = inf;
        while(l <= r) {
            int mid = (l + r) / 2;
            if (check(mid)) {
                res = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        ans = min(ans, res);
        rolate();
        // for (int i = 1; i <= h; i++) {
        //     for (int j = 1; j <= w; j++) cout << a[i][j] << ' ';
        //     cout << '\n';
        // }
        // cout << '\n';
        // return 0;
    }
    cout << ans;
    return 0;
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
