Submission #1087918

# Submission time Handle Problem Language Result Execution time Memory
1087918 2024-09-13T13:40:41 Z LOLOLO The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
1968 ms 118068 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 3e6 + 100;
int n, m, mn = 1e9, mx = 0;
vector < vector <int>> a, b, c, d;

bool check(int lim, vector < vector <int>> &a) {
    vector < vector <int>> val(n + 1, vector <int> (m + 1, 0));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j] - mn <= lim) {
                val[i][j]++;
            }

            if (mx - a[i][j] <= lim) {
                val[i][j] += 2;
            }

            if (val[i][j] == 0)
                return 0;
        }
    }

    int lst = 0, cur = m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= cur; j++) {
            if (val[i][j] == 3 || val[i][j] == lst) 
                continue;

            if (lst == 0) {
                lst = val[i][j];
            } else {
                cur = j - 1;
                break;
            }
        }

        for (int j = cur + 1; j <= m; j++) {
            if (val[i][j] != 3 && val[i][j] == lst)
                return 0;
        }
    }

    return 1;
}

void solve() {
    cin >> n >> m;
    a.assign(n + 1, vector <int> (m + 1));

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            mn = min(mn, a[i][j]);
            mx = max(mx, a[i][j]);
        }
    }

    b = a, c = a;

    for (int i = 1; i <= n / 2; i++) {
        swap(b[i], b[n - i + 1]);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m / 2; j++) {
            swap(c[i][j], c[i][m - j + 1]);
        }
    }

    d = c;
    for (int i = 1; i <= n / 2; i++) {
        swap(d[i], d[n - i + 1]);
    }

    int l = 0, r = mx, m, ans = mx - mn;
    while (l <= r) {
        m = (l + r) / 2;
        if (check(m, a) || check(m, b) || check(m, c) || check(m, d)) {
            ans = m;
            r = m - 1;
        } else {
            l = m + 1;
        }
    }

    cout << ans << '\n';
}


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

    
    int t = 1;
    //cin >> t;

    while (t--) {
        solve();
        //cout << solve() << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1292 KB Output is correct
17 Correct 11 ms 1552 KB Output is correct
18 Correct 5 ms 1680 KB Output is correct
19 Correct 10 ms 1556 KB Output is correct
20 Correct 12 ms 1428 KB Output is correct
21 Correct 14 ms 1672 KB Output is correct
22 Correct 7 ms 1652 KB Output is correct
23 Correct 19 ms 1664 KB Output is correct
24 Correct 6 ms 1528 KB Output is correct
25 Correct 27 ms 1668 KB Output is correct
26 Correct 12 ms 1668 KB Output is correct
27 Correct 13 ms 1668 KB Output is correct
28 Correct 14 ms 1672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1292 KB Output is correct
17 Correct 11 ms 1552 KB Output is correct
18 Correct 5 ms 1680 KB Output is correct
19 Correct 10 ms 1556 KB Output is correct
20 Correct 12 ms 1428 KB Output is correct
21 Correct 14 ms 1672 KB Output is correct
22 Correct 7 ms 1652 KB Output is correct
23 Correct 19 ms 1664 KB Output is correct
24 Correct 6 ms 1528 KB Output is correct
25 Correct 27 ms 1668 KB Output is correct
26 Correct 12 ms 1668 KB Output is correct
27 Correct 13 ms 1668 KB Output is correct
28 Correct 14 ms 1672 KB Output is correct
29 Correct 977 ms 97368 KB Output is correct
30 Correct 1183 ms 95084 KB Output is correct
31 Correct 1629 ms 102280 KB Output is correct
32 Correct 1010 ms 102252 KB Output is correct
33 Correct 467 ms 89308 KB Output is correct
34 Correct 1271 ms 102408 KB Output is correct
35 Correct 1485 ms 118028 KB Output is correct
36 Correct 858 ms 103168 KB Output is correct
37 Correct 1968 ms 118068 KB Output is correct