답안 #770500

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770500 2023-07-01T10:58:35 Z GrindMachine The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
642 ms 31736 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)

idea for 100 points:

minimize the maximum => think of b.s

how to write the ok() function?

let's say we are checking if ans <= k

mn and mx are in diff territories
all values in the min territory have to be <= mn+k
all values in the max territory have to be >= mx-k

the upper left col can belong to either the mn/mx territory
and the length chosen in each row could be increasing/decreasing

try out all 4 possibilities, check if possible

*/

const int MOD = 1e9 + 7;
const int N = 2e3 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

ll a[N][N];

void solve(int test_case)
{
    ll n,m; cin >> n >> m;
    rep1(i,n) rep1(j,m) cin >> 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 3 ms 1364 KB Output is correct
17 Correct 6 ms 1364 KB Output is correct
18 Correct 5 ms 1364 KB Output is correct
19 Correct 5 ms 1364 KB Output is correct
20 Correct 5 ms 1236 KB Output is correct
21 Correct 8 ms 1364 KB Output is correct
22 Correct 6 ms 1364 KB Output is correct
23 Correct 7 ms 1364 KB Output is correct
24 Correct 5 ms 1280 KB Output is correct
25 Correct 7 ms 1340 KB Output is correct
26 Correct 7 ms 1364 KB Output is correct
27 Correct 10 ms 1444 KB Output is correct
28 Correct 7 ms 1448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 3 ms 1364 KB Output is correct
17 Correct 6 ms 1364 KB Output is correct
18 Correct 5 ms 1364 KB Output is correct
19 Correct 5 ms 1364 KB Output is correct
20 Correct 5 ms 1236 KB Output is correct
21 Correct 8 ms 1364 KB Output is correct
22 Correct 6 ms 1364 KB Output is correct
23 Correct 7 ms 1364 KB Output is correct
24 Correct 5 ms 1280 KB Output is correct
25 Correct 7 ms 1340 KB Output is correct
26 Correct 7 ms 1364 KB Output is correct
27 Correct 10 ms 1444 KB Output is correct
28 Correct 7 ms 1448 KB Output is correct
29 Correct 433 ms 30136 KB Output is correct
30 Correct 486 ms 31736 KB Output is correct
31 Correct 454 ms 31664 KB Output is correct
32 Correct 500 ms 31636 KB Output is correct
33 Correct 424 ms 27856 KB Output is correct
34 Correct 489 ms 31692 KB Output is correct
35 Correct 622 ms 31612 KB Output is correct
36 Correct 622 ms 31552 KB Output is correct
37 Correct 642 ms 31700 KB Output is correct