제출 #1277769

#제출 시각아이디문제언어결과실행 시간메모리
1277769wedonttalkanymoreThe Kingdom of JOIOI (JOI17_joioi)C++20
100 / 100
675 ms63672 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;
}

컴파일 시 표준 에러 (stderr) 메시지

joioi.cpp: In function 'int main()':
joioi.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
joioi.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...