Submission #226684

# Submission time Handle Problem Language Result Execution time Memory
226684 2020-04-24T19:44:57 Z DS007 The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
1589 ms 58884 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int H = 2000;
int a[H][H];
int h, w, mi, ma;

void pre() {
    mi = 1e18, ma = -1;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++)
            mi = min(mi, a[i][j]), ma = max(ma, a[i][j]);
    }
}

bool check(int dif) {
    int ll = mi + dif + 1, ul = ma - dif - 1;
    bool track[h][w] = {};

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] >= ll)
                track[i][j] = true;
        }
    }

    for (int i = 0; i < h; i++) {
        int li = w, hi = -1;
        for (int j = 0; j < w; j++) {
            if (track[i][j])
                li = min(li, j), hi = max(hi, j);
        }

        for (int j = li; j <= hi; j++) {
            if (a[i][j] <= ul)
                return false;
            track[i][j] = true;
        }
    }

    for (int j = 0; j < w; j++) {
        int li = h, hi = -1;
        for (int i = 0; i < h; i++) {
            if (track[i][j])
                li = min(li, i), hi = max(hi, i);
        }

        for (int i = li; i <= hi; i++) {
            if (a[i][j] <= ul)
                return false;
            track[i][j] = true;
        }
    }

    bool check = true;
    for (int i = h - 1, max_ind = -1; i >= 0; i--) {
        for (int j = 0; j < w; j++) {
            if (track[i][j])
                max_ind = max(max_ind, j);
        }
        for (int j = 0; j <= max_ind; j++) {
            if (a[i][j] <= ul)
                check = false;
        }
    }

    if (check)
        return true;
    check = true;

    for (int i = 0, max_ind = -1; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (track[i][j])
                max_ind = max(max_ind, j);
        }
        for (int j = 0; j <= max_ind; j++) {
            if (a[i][j] <= ul)
                check = false;
        }
    }

    if (check)
        return true;
    check = true;

    for (int i = h - 1, min_ind = w; i >= 0; i--) {
        for (int j = 0; j < w; j++) {
            if (track[i][j])
                min_ind = min(min_ind, j);
        }
        for (int j = w - 1; j >= min_ind; j--) {
            if (a[i][j] <= ul)
                check = false;
        }
    }

    if (check)
        return true;
    check = true;

    for (int i = 0, min_ind = w; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (track[i][j])
                min_ind = min(min_ind, j);
        }
        for (int j = w - 1; j >= min_ind; j--) {
            if (a[i][j] <= ul)
                check = false;
        }
    }

    if (check)
        return true;
    return false;
}

void solveTestCase() {
    cin >> h >> w;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++)
            cin >> a[i][j];
    }
    pre();

    int lo = 0, hi = 1e9, ans = 1e9;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        if (check(mid))
            ans = mid, hi = mid - 1;
        else
            lo = mid + 1;
    }

    cout << ans;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int test = 1;
    // cin >> test;
    for (int i = 1; i <= test; i++)
        solveTestCase();
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 12 ms 1536 KB Output is correct
17 Correct 17 ms 1792 KB Output is correct
18 Correct 17 ms 1792 KB Output is correct
19 Correct 17 ms 1792 KB Output is correct
20 Correct 15 ms 1536 KB Output is correct
21 Correct 19 ms 1920 KB Output is correct
22 Correct 14 ms 1792 KB Output is correct
23 Correct 20 ms 2048 KB Output is correct
24 Correct 18 ms 1792 KB Output is correct
25 Correct 23 ms 1920 KB Output is correct
26 Correct 18 ms 1920 KB Output is correct
27 Correct 18 ms 1920 KB Output is correct
28 Correct 18 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 12 ms 1536 KB Output is correct
17 Correct 17 ms 1792 KB Output is correct
18 Correct 17 ms 1792 KB Output is correct
19 Correct 17 ms 1792 KB Output is correct
20 Correct 15 ms 1536 KB Output is correct
21 Correct 19 ms 1920 KB Output is correct
22 Correct 14 ms 1792 KB Output is correct
23 Correct 20 ms 2048 KB Output is correct
24 Correct 18 ms 1792 KB Output is correct
25 Correct 23 ms 1920 KB Output is correct
26 Correct 18 ms 1920 KB Output is correct
27 Correct 18 ms 1920 KB Output is correct
28 Correct 18 ms 1920 KB Output is correct
29 Correct 1014 ms 55780 KB Output is correct
30 Correct 1110 ms 56824 KB Output is correct
31 Correct 1544 ms 58884 KB Output is correct
32 Correct 1094 ms 58872 KB Output is correct
33 Correct 963 ms 51320 KB Output is correct
34 Correct 1326 ms 58868 KB Output is correct
35 Correct 1574 ms 50728 KB Output is correct
36 Correct 1551 ms 47612 KB Output is correct
37 Correct 1589 ms 50552 KB Output is correct