답안 #767335

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
767335 2023-06-26T15:57:22 Z sysia The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
607 ms 70660 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

void solve(){
    int n, m; cin >> n >> m;
    vector a(n+1, vector<int>(m+1));
    int mx = -oo, mn = oo;
    T A, B;
    for (int i = 1; i<=n; i++){
        for (int j = 1; j<=m; j++){
            cin >> a[i][j];
            if (a[i][j] > mx){
                mx = a[i][j];
                A = {i, j};
            }
            if (a[i][j] < mn){
                mn = a[i][j];
                B = {i, j};
            }
        }
    }
    if (mn == mx){
        cout << "0\n";
        return;
    }
    int ans = mx-mn;
    auto sortuj = [&](vector<int>L, vector<int>R){
        debug(L, R); //a[i] \in [L[i], R[i]]
        //1) increasing
        bool ok = 1;
        vector<int>tab(n+1);
        for (int i = 1; i<=n; i++){
            tab[i] = L[i];
            if (i > 1) tab[i] = max(tab[i], tab[i-1]);
            if (tab[i] > R[i] || tab[i] < L[i]){
                ok = 0;
                break;
            }
        }
        if (ok) return true;
        ok = 1;
        //2) decreasing
        for (int i = 1; i<=n; i++){
            tab[i] = R[i];
            if (i > 1) tab[i] = min(tab[i], tab[i-1]);
            if (tab[i] > R[i] || tab[i] < L[i]){
                ok = 0;
                break;
            }
        }
        return ok;
    };
    
    auto check = [&](int k){
        //max(A-mn, mx-B) = k;
        //A <= k + mn
        //B >= mx - k
        debug(k+mn, mx-k);
        //sprawdzic czy da sie przypisac
        //0) z lewa minimum
        //1) z lewa maksimum
        for (int rep = 0; rep < 2; rep++){
            vector<int>R(n+1, m), L(n+1, 0);
            bool ok = 1;
            for (int i = 1; i<=n; i++){
                for (int j = 1; j<=m; j++){
                    if ((!rep && a[i][j] > k + mn) || (rep && a[i][j] < mx - k)){
                        R[i] = j-1;
                        break;
                    }
                }
                for (int j = R[i]+1; j<=m; j++){
                    if (!rep) ok &= (a[i][j] >= mx-k);
                    else ok &= (a[i][j] <= k+mn);
                }
                for (int j = R[i]; j>=1; j--){
                    if ((!rep && a[i][j] < mx-k) || (rep && a[i][j] > k+mn)){
                        L[i] = j;
                        break;
                    }
                }
            }
            if (ok && sortuj(L, R)) return true;
        }
        return false;
    };

    //ans = max(mx-A, B-mn);
    int l = 1, r = oo;
    while (r >= l){
        int mid = (l+r)/2;
        if (check(mid)){
            ans = mid;
            r = mid-1;
        } else {
            l = mid+1;
        }
    }
    ans = min(ans, mx-mn);
    cout << ans << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 320 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 320 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 324 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 5 ms 852 KB Output is correct
18 Correct 4 ms 848 KB Output is correct
19 Correct 4 ms 852 KB Output is correct
20 Correct 4 ms 724 KB Output is correct
21 Correct 6 ms 980 KB Output is correct
22 Correct 5 ms 980 KB Output is correct
23 Correct 6 ms 976 KB Output is correct
24 Correct 4 ms 956 KB Output is correct
25 Correct 5 ms 980 KB Output is correct
26 Correct 5 ms 1032 KB Output is correct
27 Correct 6 ms 972 KB Output is correct
28 Correct 6 ms 1020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 320 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 324 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 5 ms 852 KB Output is correct
18 Correct 4 ms 848 KB Output is correct
19 Correct 4 ms 852 KB Output is correct
20 Correct 4 ms 724 KB Output is correct
21 Correct 6 ms 980 KB Output is correct
22 Correct 5 ms 980 KB Output is correct
23 Correct 6 ms 976 KB Output is correct
24 Correct 4 ms 956 KB Output is correct
25 Correct 5 ms 980 KB Output is correct
26 Correct 5 ms 1032 KB Output is correct
27 Correct 6 ms 972 KB Output is correct
28 Correct 6 ms 1020 KB Output is correct
29 Correct 439 ms 52300 KB Output is correct
30 Correct 469 ms 50956 KB Output is correct
31 Correct 498 ms 54908 KB Output is correct
32 Correct 485 ms 54892 KB Output is correct
33 Correct 413 ms 47820 KB Output is correct
34 Correct 471 ms 55000 KB Output is correct
35 Correct 579 ms 70548 KB Output is correct
36 Correct 507 ms 61516 KB Output is correct
37 Correct 607 ms 70660 KB Output is correct