Submission #770498

# Submission time Handle Problem Language Result Execution time Memory
770498 2023-07-01T10:54:05 Z GrindMachine The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
707 ms 196448 KB
// Om Namah Shivaya
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
 
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
 
template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}
 
template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
 
/*
 
refs:
https://codeforces.com/blog/entry/50413

(for 60 points):
stress tested to identify the bug: https://oj.uz/submission/767335
didnt set the bounds for the binary search properly (wrote n instead of m)

*/

const int MOD = 1e9 + 7;
const int N = 2e3 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
 
ll n,m;
ll a[N][N], pmn[N][N], pmx[N][N], smn[N][N], smx[N][N];
 
void solve(int test_case)
{
    cin >> n >> m;
    rep1(i,n) rep1(j,m) cin >> a[i][j];
 
    memset(pmn,0x10,sizeof pmn);
    memset(smn,0x10,sizeof smn);
    memset(pmx,-0x10,sizeof pmx);
    memset(smx,-0x10,sizeof smx);
 
    rep1(i,n){
        rep1(j,m){
            pmn[i][j] = min(pmn[i][j-1], a[i][j]);
            pmx[i][j] = max(pmx[i][j-1], a[i][j]);
        }
        rev(j,m,1){
            smn[i][j] = min(smn[i][j+1], a[i][j]);
            smx[i][j] = max(smx[i][j+1], a[i][j]);
        }
    }

    ll mn = inf2, mx = -inf2;
    rep1(i,n){
        rep1(j,m){
            amin(mn,a[i][j]);
            amax(mx,a[i][j]);
        }
    }

    auto ok = [&](ll k){
        // min territory, decreasing
        {
            ll prev_len = inf2;
            bool flag = true;

            rep1(i,n){
                ll len = 0;
                rep1(j,m){
                    if(a[i][j] <= mn+k){
                        len++;
                    }
                    else{
                        break;
                    }
                }

                amin(len,prev_len);
                prev_len = len;

                for(int j = len+1; j <= m; ++j){
                    if(a[i][j] < mx-k){
                        flag = false;
                    }
                }
            }

            if(flag) return true;
        }

        // min territory, decreasing
        {
            ll prev_len = inf2;
            bool flag = true;

            rev(i,n,1){
                ll len = 0;
                rep1(j,m){
                    if(a[i][j] <= mn+k){
                        len++;
                    }
                    else{
                        break;
                    }
                }

                amin(len,prev_len);
                prev_len = len;

                for(int j = len+1; j <= m; ++j){
                    if(a[i][j] < mx-k){
                        flag = false;
                    }
                }
            }

            if(flag) return true;
        }

        // max territory, decreasing
        {
            ll prev_len = inf2;
            bool flag = true;

            rep1(i,n){
                ll len = 0;
                rep1(j,m){
                    if(a[i][j] >= mx-k){
                        len++;
                    }
                    else{
                        break;
                    }
                }

                amin(len,prev_len);
                prev_len = len;

                for(int j = len+1; j <= m; ++j){
                    if(a[i][j] > mn+k){
                        flag = false;
                    }
                }
            }

            if(flag) return true;
        }

        // max territory, increasing
        {
            ll prev_len = inf2;
            bool flag = true;

            rev(i,n,1){
                ll len = 0;
                rep1(j,m){
                    if(a[i][j] >= mx-k){
                        len++;
                    }
                    else{
                        break;
                    }
                }

                amin(len,prev_len);
                prev_len = len;

                for(int j = len+1; j <= m; ++j){
                    if(a[i][j] > mn+k){
                        flag = false;
                    }
                }
            }

            if(flag) return true;
        }
        return false;
    };

    ll l = 0, r = inf1;
    ll ans = -1;

    while(l <= r){
        ll mid = (l+r) >> 1;
        if(ok(mid)){
            ans = mid;
            r = mid-1;
        }
        else{
            l = mid+1;
        }
    }    

    cout << ans << endl;
}
 
int main()
{
    fastio;
 
    int t = 1;
    // cin >> t;
 
    rep1(i, t) {
        solve(i);
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 38 ms 126116 KB Output is correct
2 Correct 37 ms 126208 KB Output is correct
3 Correct 38 ms 126188 KB Output is correct
4 Correct 38 ms 126148 KB Output is correct
5 Correct 38 ms 126224 KB Output is correct
6 Correct 39 ms 126180 KB Output is correct
7 Correct 38 ms 126128 KB Output is correct
8 Correct 39 ms 126168 KB Output is correct
9 Correct 38 ms 126156 KB Output is correct
10 Correct 38 ms 126100 KB Output is correct
11 Correct 39 ms 126148 KB Output is correct
12 Correct 39 ms 126108 KB Output is correct
13 Correct 42 ms 126172 KB Output is correct
14 Correct 44 ms 126184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 126116 KB Output is correct
2 Correct 37 ms 126208 KB Output is correct
3 Correct 38 ms 126188 KB Output is correct
4 Correct 38 ms 126148 KB Output is correct
5 Correct 38 ms 126224 KB Output is correct
6 Correct 39 ms 126180 KB Output is correct
7 Correct 38 ms 126128 KB Output is correct
8 Correct 39 ms 126168 KB Output is correct
9 Correct 38 ms 126156 KB Output is correct
10 Correct 38 ms 126100 KB Output is correct
11 Correct 39 ms 126148 KB Output is correct
12 Correct 39 ms 126108 KB Output is correct
13 Correct 42 ms 126172 KB Output is correct
14 Correct 44 ms 126184 KB Output is correct
15 Correct 39 ms 126144 KB Output is correct
16 Correct 41 ms 127264 KB Output is correct
17 Correct 44 ms 127216 KB Output is correct
18 Correct 43 ms 127220 KB Output is correct
19 Correct 44 ms 127240 KB Output is correct
20 Correct 43 ms 127092 KB Output is correct
21 Correct 47 ms 127272 KB Output is correct
22 Correct 45 ms 127276 KB Output is correct
23 Correct 45 ms 127276 KB Output is correct
24 Correct 44 ms 127052 KB Output is correct
25 Correct 46 ms 127272 KB Output is correct
26 Correct 48 ms 127236 KB Output is correct
27 Correct 45 ms 127264 KB Output is correct
28 Correct 46 ms 127180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 126116 KB Output is correct
2 Correct 37 ms 126208 KB Output is correct
3 Correct 38 ms 126188 KB Output is correct
4 Correct 38 ms 126148 KB Output is correct
5 Correct 38 ms 126224 KB Output is correct
6 Correct 39 ms 126180 KB Output is correct
7 Correct 38 ms 126128 KB Output is correct
8 Correct 39 ms 126168 KB Output is correct
9 Correct 38 ms 126156 KB Output is correct
10 Correct 38 ms 126100 KB Output is correct
11 Correct 39 ms 126148 KB Output is correct
12 Correct 39 ms 126108 KB Output is correct
13 Correct 42 ms 126172 KB Output is correct
14 Correct 44 ms 126184 KB Output is correct
15 Correct 39 ms 126144 KB Output is correct
16 Correct 41 ms 127264 KB Output is correct
17 Correct 44 ms 127216 KB Output is correct
18 Correct 43 ms 127220 KB Output is correct
19 Correct 44 ms 127240 KB Output is correct
20 Correct 43 ms 127092 KB Output is correct
21 Correct 47 ms 127272 KB Output is correct
22 Correct 45 ms 127276 KB Output is correct
23 Correct 45 ms 127276 KB Output is correct
24 Correct 44 ms 127052 KB Output is correct
25 Correct 46 ms 127272 KB Output is correct
26 Correct 48 ms 127236 KB Output is correct
27 Correct 45 ms 127264 KB Output is correct
28 Correct 46 ms 127180 KB Output is correct
29 Correct 497 ms 155916 KB Output is correct
30 Correct 527 ms 179032 KB Output is correct
31 Correct 513 ms 180804 KB Output is correct
32 Correct 546 ms 180604 KB Output is correct
33 Correct 439 ms 173768 KB Output is correct
34 Correct 539 ms 180844 KB Output is correct
35 Correct 705 ms 196328 KB Output is correct
36 Correct 693 ms 191152 KB Output is correct
37 Correct 707 ms 196448 KB Output is correct