Submission #1284058

#TimeUsernameProblemLanguageResultExecution timeMemory
1284058baotoan655The Kingdom of JOIOI (JOI17_joioi)C++20
100 / 100
1004 ms31836 KiB
#include <bits/stdc++.h>
#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;
#include <bits/stdc++.h>
using namespace std;

const int N = 2005;

int n, m, mn = 1e9, a[N][N], b[N][N], f[N];

bool check0(int x) {
    f[0] = 1e9;
    int _mn, _mx;
    _mn = 1e9, _mx = 0;
    for (int i = 1; i <= n; ++i) {
        int mx = 0;
        f[i] = 0;
        for (int j = 1; j <= m; ++j) {
            mx = max(mx, b[i][j]);
            if (mx - mn > x) break;
            f[i]++;
        }
        f[i] = min(f[i], f[i - 1]);
        for (int j = f[i] + 1; j <= m; ++j) {
            _mn = min(_mn, b[i][j]);
            _mx = max(_mx, b[i][j]);
        }
    }
    return _mx - _mn <= x;
}

bool check1(int x) {
    bool ok = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            b[i][j] = a[i][j];
        }
    }
    ok |= check0(x);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            b[i][j] = a[i][m - j + 1];
        }
    }
    ok |= check0(x);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            b[i][j] = a[n - i + 1][j];
        }
    }
    ok |= check0(x);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            b[i][j] = a[n - i + 1][m - j + 1];
        }
    }
    ok |= check0(x);
    return ok;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    file("A") else file("task");
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> a[i][j], mn = min(mn, a[i][j]);
        }
    }
    int l = 0, r = 1e9, mid, res;
    while (l <= r) {
        mid = l + r >> 1;
        if (check1(mid)) r = (res = mid) - 1;
        else l = mid + 1;
    }
    cout << l;
    return 0;
}

Compilation message (stderr)

joioi.cpp: In function 'int main()':
joioi.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:66:5: note: in expansion of macro 'file'
   66 |     file("A") else file("task");
      |     ^~~~
joioi.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:66:5: note: in expansion of macro 'file'
   66 |     file("A") else file("task");
      |     ^~~~
joioi.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:66:20: note: in expansion of macro 'file'
   66 |     file("A") else file("task");
      |                    ^~~~
joioi.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:66:20: note: in expansion of macro 'file'
   66 |     file("A") else file("task");
      |                    ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...