답안 #939276

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939276 2024-03-06T08:03:40 Z qwe1rt1yuiop1 The Kingdom of JOIOI (JOI17_joioi) C++14
0 / 100
0 ms 600 KB
#include <bits/stdc++.h>
// #define int long long
using namespace std;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> v(n, vector<int>(m));
    for (auto &i : v)
        for (auto &j : i)
            cin >> j;

    int l = 0, r = 1000000005;
    while (l < r)
    {
        int mid = (l + r) >> 1, ok = 1;

        vector<int> a(n), b(n), c(m), d(m);

        for (int i = 0; i < n; ++i)
        {
            int mx = v[i][0], mn = mx;
            for (a[i] = 0; a[i] < m; ++a[i])
            {
                mx = max(mx, v[i][a[i]]);
                mn = min(mn, v[i][a[i]]);
                if (mx - mn > mid)
                    break;
            }
            --a[i];
        }
        for (int i = 0; i < n; ++i)
        {
            int mx = v[i][m - 1], mn = mx;
            for (b[i] = m - 1; b[i] >= 0; --b[i])
            {
                mx = max(mx, v[i][b[i]]);
                mn = min(mn, v[i][b[i]]);
                if (mx - mn > mid)
                    break;
            }
            ++b[i];
            if (b[i] > a[i] + 1)
            {
                ok = 0;
                break;
            }
        }

        if (ok == 0)
        {
            l = mid + 1;
            continue;
        }

        for (int i = 0; i < m; ++i)
        {
            int mx = v[0][i], mn = mx;
            for (c[i] = 0; c[i] < n; ++c[i])
            {
                mx = max(mx, v[c[i]][i]);
                mn = min(mn, v[c[i]][i]);
                if (mx - mn > mid)
                    break;
            }
            --c[i];
        }
        for (int i = 0; i < m; ++i)
        {
            int mx = v[n - 1][i], mn = mx;
            for (d[i] = n - 1; d[i] >= 0; --d[i])
            {
                mx = max(mx, v[d[i]][i]);
                mn = min(mn, v[d[i]][i]);
                if (mx - mn > mid)
                    break;
            }
            ++d[i];
            if (d[i] > c[i] + 1)
            {
                ok = 0;
                break;
            }
        }

        if (ok == 0)
        {
            l = mid + 1;
            continue;
        }

        auto c0 = c, d0 = d;
        for (int i = 1; i < m; ++i)
            c[i] = min(c[i], c[i - 1]);
        for (int i = m - 2; i >= 0; --i)
            d[i] = max(d[i], d[i + 1]);

        int mn = a[0];
        for (int i = 0; i < n; ++i)
        {
            mn = min(mn, a[i]);
            while (mn >= 0 && (c[mn] < i || mn < m && d[mn + 1] > i))
                --mn;
            if (b[i] > mn + 1)
            {
                ok = 0;
                break;
            }
        }
        if (ok)
        {
            r = mid;
            continue;
        }
        c = c0, d = d0;
        for (int i = m - 2; i >= 0; --i)
            c[i] = min(c[i], c[i + 1]);
        for (int i = 1; i < m; ++i)
            d[i] = max(d[i], d[i - 1]);
        int mx = b[0];
        for (int i = 0; i < n; ++i)
        {
            mx = max(mx, b[i]);
            while (mx < m && (c[mx] < i || mx > 0 && d[mx - 1] > i))
                ++mx;
            if (mx > a[i] + 1)
            {
                ok = 0;
                break;
            }
        }

        if (ok)
            r = mid;
        else
            l = mid + 1;
    }
    cout << l << '\n';
}

/*
4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10

8 6
23 23 10 11 16 21
15 26 19 28 19 20
25 26 28 16 15 11
11 8 19 11 15 24
14 19 15 14 24 11
10 8 11 7 6 14
23 5 19 23 17 17
18 11 21 14 20 16

 */

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}

Compilation message

joioi.cpp: In function 'void solve()':
joioi.cpp:103:52: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  103 |             while (mn >= 0 && (c[mn] < i || mn < m && d[mn + 1] > i))
      |                                             ~~~~~~~^~~~~~~~~~~~~~~~
joioi.cpp:125:51: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  125 |             while (mx < m && (c[mx] < i || mx > 0 && d[mx - 1] > i))
      |                                            ~~~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 596 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 596 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 596 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -