답안 #939995

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

int n, m, mx, mn;
vector<vector<int>> v;

int f1(int mid)
{
    vector<int> tmp(n);
    int x = 0;
    for (int i = 0; i < n; ++i)
    {
        int cur = m - 1;
        for (; cur >= 0; --cur)
            if (v[i][cur] < mx - mid)
                break;
        ++cur;
        x = max(x, cur);
        tmp[i] = x;
        for (int j = 0; j < x; ++j)
            if (v[i][j] > mn + mid)
                return 0;
        for (int j = x; j < m; ++j)
            if (v[i][j] < mx - mid)
                return 0;
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < tmp[i]; ++j)
            assert(v[i][j] <= mn + mid);
        for (int j = tmp[i]; j < m; ++j)
            assert(v[i][j] >= mx - mid);
    }
    return 1;
}

int f2(int mid)
{
    vector<int> tmp(n);
    int x = 0;
    for (int i = 0; i < n; ++i)
    {
        int cur = m - 1;
        for (; cur >= 0; --cur)
            if (v[i][cur] > mn + mid)
                break;
        ++cur;
        x = max(x, cur);
        tmp[i] = x;
        for (int j = 0; j < x; ++j)
            if (v[i][j] < mx - mid)
                return 0;
        for (int j = x; j < m; ++j)
            if (v[i][j] > mn + mid)
                return 0;
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < tmp[i]; ++j)
            assert(v[i][j] >= mx - mid);
        for (int j = tmp[i]; j < m; ++j)
            assert(v[i][j] <= mn + mid);
    }
    return 1;
}

int f3(int mid)
{
    vector<int> tmp(n);
    int x = m - 1;
    for (int i = 0; i < n; ++i)
    {
        int cur = 0;
        for (; cur < m; ++cur)
            if (v[i][cur] < mx - mid)
                break;
        --cur;
        x = min(x, cur);
        tmp[i] = x;
        for (int j = x + 1; j < m; ++j)
            if (v[i][j] > mn + mid)
                return 0;
        for (int j = 0; j <= x; ++j)
            if (v[i][j] < mx - mid)
                return 0;
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= tmp[i]; ++j)
            assert(v[i][j] >= mx - mid);
        for (int j = tmp[i] + 1; j < m; ++j)
            assert(v[i][j] <= mn + mid);
    }
    return 1;
}

int f4(int mid)
{
    vector<int> tmp(n);
    int x = m - 1;
    for (int i = 0; i < n; ++i)
    {
        int cur = 0;
        for (; cur < m; ++cur)
            if (v[i][cur] > mn + mid)
                break;
        --cur;
        x = min(x, cur);
        tmp[i] = x;
        for (int j = x + 1; j < m; ++j)
            if (v[i][j] < mx - mid)
                return 0;
        for (int j = 0; j <= x; ++j)
            if (v[i][j] > mn + mid)
                return 0;
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= tmp[i]; ++j)
            assert(v[i][j] <= mn + mid);
        for (int j = tmp[i] + 1; j < m; ++j)
            assert(v[i][j] >= mx - mid);
    }
    return 1;
}

void solve()
{
    cin >> n >> m;
    v.assign(n, vector<int>(m));
    mx = 0, mn = INT_MAX;
    for (auto &i : v)
        for (auto &j : i)
            cin >> j, mx = max(mx, j), mn = min(mn, j);

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

        if (f1(mid) || f2(mid) || f3(mid) || f4(mid))
            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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 452 KB Output is correct
5 Correct 1 ms 348 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 452 KB Output is correct
5 Correct 1 ms 348 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 4 ms 860 KB Output is correct
18 Correct 4 ms 724 KB Output is correct
19 Correct 4 ms 860 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 5 ms 1008 KB Output is correct
22 Correct 4 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 4 ms 860 KB Output is correct
25 Correct 5 ms 860 KB Output is correct
26 Correct 5 ms 860 KB Output is correct
27 Correct 5 ms 860 KB Output is correct
28 Correct 5 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 452 KB Output is correct
5 Correct 1 ms 348 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 4 ms 860 KB Output is correct
18 Correct 4 ms 724 KB Output is correct
19 Correct 4 ms 860 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 5 ms 1008 KB Output is correct
22 Correct 4 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 4 ms 860 KB Output is correct
25 Correct 5 ms 860 KB Output is correct
26 Correct 5 ms 860 KB Output is correct
27 Correct 5 ms 860 KB Output is correct
28 Correct 5 ms 860 KB Output is correct
29 Correct 306 ms 37256 KB Output is correct
30 Correct 344 ms 36496 KB Output is correct
31 Correct 357 ms 39328 KB Output is correct
32 Correct 340 ms 39088 KB Output is correct
33 Correct 309 ms 33988 KB Output is correct
34 Correct 345 ms 39080 KB Output is correct
35 Correct 417 ms 54960 KB Output is correct
36 Correct 340 ms 47716 KB Output is correct
37 Correct 380 ms 54560 KB Output is correct