Submission #849027

#TimeUsernameProblemLanguageResultExecution timeMemory
849027anha3k25cvpThe Kingdom of JOIOI (JOI17_joioi)C++14
100 / 100
1403 ms70780 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>

using namespace std;

const int N = 5 + 1e5;
const int inf = 7 + 1e9;

int m, n, ma = 0;
vector <vector <int>> a;

int check(int lim) {
    vector <int> len(m + 1, n);
    for (int i = 1; i <= m; i ++)
        for (int j = n; j > 0; j --) {
            if (a[i][j] < ma - lim)
                break;
            len[i] --;
        }
    int val = 0, x = inf, y = 0, check = 0;
    for (int i = 1; i <= m; i ++) {
        val = max(val, len[i]);
        if (val < n)
            check = 1;
        for (int j = 1; j <= val; j ++) {
            x = min(x, a[i][j]);
            y = max(y, a[i][j]);
        }
    }
    if (check && y - x <= lim)
        return 1;
    val = 0, x = inf, y = 0, check = 0;
    for (int i = m; i > 0; i --) {
        val = max(val, len[i]);
        if (val < n)
            check = 1;
        for (int j = 1; j <= val; j ++) {
            x = min(x, a[i][j]);
            y = max(y, a[i][j]);
        }
    }
    return check && y - x <= lim;
}

int cal() {
    vector <vector <int>> b = a;
    for (int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j ++)
            b[j][m - i + 1] = a[i][j];
    a = b;
    swap(m, n);
    int lo = 0, hi = inf;
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        if (check(mid))
            hi = mid;
        else
            lo = mid + 1;
    }
    return lo;
}

int main() {
#define TASKNAME ""
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    if ( fopen( TASKNAME".inp", "r" ) ) {
        freopen (TASKNAME".inp", "r", stdin);
        freopen (TASKNAME".out", "w", stdout);
    }
    cin >> m >> n;
    a.assign(max(m, n) + 1, vector <int> (max(m, n) + 1, 0));
    for (int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j ++) {
            cin >> a[i][j];
            ma = max(ma, a[i][j]);
        }
    int ans = inf;
    for (int i = 0; i < 4; i ++)
        ans = min(ans, cal());
    cout << ans;
    return 0;
}

Compilation message (stderr)

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