Submission #757675

# Submission time Handle Problem Language Result Execution time Memory
757675 2023-06-13T14:40:04 Z Kihihihi The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
2560 ms 65600 KB
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <ctime>
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <fstream>
#include <functional>
#include <complex>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

const long long INF = 1e18, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 18;
const long double EPS = 1e-9, PI = acos(-1);

void solve()
{
    long long n, m;
    vector<vector<long long>> v;
    cin >> n >> m;
    v.resize(n, vector<long long>(m));
    for (long long i = 0; i < n; i++)
    {
        for (long long j = 0; j < m; j++)
        {
            cin >> v[i][j];
        }
    }

    long long mn = INF, mx = -INF;
    for (long long i = 0; i < n; i++)
    {
        for (long long j = 0; j < m; j++)
        {
            mn = min(mn, v[i][j]);
            mx = max(mx, v[i][j]);
        }
    }

    auto build = [&](vector<vector<long long>>& col) -> bool
    {
        vector<long long> left(n, -1);
        for (long long i = 0; i < n; i++)
        {
            for (long long j = 0; j < m; j++)
            {
                if (col[i][j] == 0)
                {
                    left[i] = j;
                }
            }
        }
        for (long long i = 1; i < n; i++)
        {
            left[i] = max(left[i], left[i - 1]);
        }
        bool success = true;
        for (long long i = 0; i < n; i++)
        {
            for (long long j = 0; j < m; j++)
            {
                if (col[i][j] == 1 && j <= left[i])
                {
                    success = false;
                    break;
                }
            }
        }
        if (success)
        {
            return true;
        }

        vector<long long> right(n, m);
        for (long long i = 0; i < n; i++)
        {
            for (long long j = m - 1; j > -1; j--)
            {
                if (col[i][j] == 0)
                {
                    right[i] = j;
                }
            }
        }
        for (long long i = 1; i < n; i++)
        {
            right[i] = min(right[i], right[i - 1]);
        }
        success = true;
        for (long long i = 0; i < n; i++)
        {
            for (long long j = 0; j < m; j++)
            {
                if (col[i][j] == 1 && right[i] <= j)
                {
                    success = false;
                    break;
                }
            }
        }
        if (success)
        {
            return true;
        }
        return false;
    };

    auto check = [&](long long mid) -> bool
    {
        vector<vector<long long>> col(n, vector<long long>(m, -1));
        long long l0 = mn, r0 = mn + mid, l1 = mx - mid, r1 = mx;
        for (long long i = 0; i < n; i++)
        {
            for (long long j = 0; j < m; j++)
            {
                if (l1 <= v[i][j] && v[i][j] <= r0)
                {
                    continue;
                }

                if (l0 <= v[i][j] && v[i][j] <= r0)
                {
                    col[i][j] = 0;
                }
                else if (l1 <= v[i][j] && v[i][j] <= r1)
                {
                    col[i][j] = 1;
                }
                else
                {
                    return false;
                }
            }
        }

        if (build(col))
        {
            return true;
        }
        for (long long i = 0; i < n; i++)
        {
            for (long long j = 0; j < m; j++)
            {
                if (col[i][j] == -1)
                {
                    continue;
                }

                col[i][j] ^= 1;
            }
        }
        if (build(col))
        {
            return true;
        }
        return false;
    };
    
    long long left = -1, right = mx - mn;
    while (right - left > 1)
    {
        long long mid = (left + right) / 2;
        if (check(mid))
        {
            right = mid;
        }
        else
        {
            left = mid;
        }
    }
    cout << right << "\n";
}

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

    int tst = 1;
    //cin >> tst;
    while (tst--)
    {
        solve();
    }

    return 0;
}

/*
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 10 ms 980 KB Output is correct
17 Correct 14 ms 1232 KB Output is correct
18 Correct 8 ms 1224 KB Output is correct
19 Correct 17 ms 1248 KB Output is correct
20 Correct 17 ms 1124 KB Output is correct
21 Correct 19 ms 1352 KB Output is correct
22 Correct 10 ms 1344 KB Output is correct
23 Correct 26 ms 1356 KB Output is correct
24 Correct 11 ms 1228 KB Output is correct
25 Correct 35 ms 1356 KB Output is correct
26 Correct 17 ms 1352 KB Output is correct
27 Correct 21 ms 1348 KB Output is correct
28 Correct 21 ms 1436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 10 ms 980 KB Output is correct
17 Correct 14 ms 1232 KB Output is correct
18 Correct 8 ms 1224 KB Output is correct
19 Correct 17 ms 1248 KB Output is correct
20 Correct 17 ms 1124 KB Output is correct
21 Correct 19 ms 1352 KB Output is correct
22 Correct 10 ms 1344 KB Output is correct
23 Correct 26 ms 1356 KB Output is correct
24 Correct 11 ms 1228 KB Output is correct
25 Correct 35 ms 1356 KB Output is correct
26 Correct 17 ms 1352 KB Output is correct
27 Correct 21 ms 1348 KB Output is correct
28 Correct 21 ms 1436 KB Output is correct
29 Correct 1370 ms 61276 KB Output is correct
30 Correct 1400 ms 59804 KB Output is correct
31 Correct 1753 ms 64164 KB Output is correct
32 Correct 1154 ms 64424 KB Output is correct
33 Correct 712 ms 56308 KB Output is correct
34 Correct 1620 ms 64632 KB Output is correct
35 Correct 1741 ms 65436 KB Output is correct
36 Correct 1031 ms 56952 KB Output is correct
37 Correct 2560 ms 65600 KB Output is correct