제출 #1000158

#제출 시각아이디문제언어결과실행 시간메모리
1000158mispertionThe Kingdom of JOIOI (JOI17_joioi)C++17
60 / 100
4006 ms117584 KiB
#include<bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"

const ld PI = 3.1415926535;
const int N = 5000+10;
const int M = 7e6 + 1;
int mod = 998244353;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 2;

int mult(int a, int b) {
    return a * 1LL * b % mod;
}

int sum(int a, int b) { 
    if (a + b < 0)
        return a + b + mod;
    if (a + b >= mod)
        return a + b - mod;
    return a + b;
}

ll binpow(ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1) {
        return binpow(a, n - 1) * a % mod;
    } else {
        ll b = binpow(a, n / 2);
        return b * b % mod;
    }
}

int H, W;
vector<vector<int>> mat = {};

void rotate(){
    vector<vector<int>> nm(sz(mat[0]), vector<int>(sz(mat)));
    for(int i = 0; i < sz(mat); i++){
        for(int j = 0; j < sz(mat[i]); j++){
            nm[sz(mat[i]) - j - 1][i] = mat[i][j];
        }
    }
    mat = nm;
}

bool check(int mxd){
    int ndmn = infi;
    for(int i = 0; i < sz(mat); i++){
        for(int j = 0; j < sz(mat[i]); j++){
            ndmn = min(ndmn, mat[i][j]);
        }
    }
    int mx = mxd + ndmn;
    int cmx = sz(mat[0]) - 1;
    int dmn = infi, dmx = -infi;
    for(int i = 0; i < sz(mat); i++){
        int ha = 0;
        for(; ha <= cmx; ha++){
            if(mat[i][ha] > mx)
                break;
        }
        cmx = ha - 1;
        //cout << ha << '\n';
        for(int j = ha; j < sz(mat[i]); j++){
            dmn = min(dmn, mat[i][j]);
            dmx = max(dmx, mat[i][j]);
        }
    }
    //cout << '\n';
    return (dmx - dmn <= mxd);
}

void solve(){
    cin >> H >> W;
    mat.assign(H, vector<int>(W, 0));
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++)
            cin >> mat[i][j];
    }
    /*cout << '\n';
    for(int k = 0; k < 4; k++){
        for(int i = 0; i < sz(mat); i++)
            for(int j = 0; j < sz(mat[i]); j++)
                cout << mat[i][j] << " \n"[sz(mat[i]) - 1 == j];
        rotate();
        cout << '\n';
    }*/
    int lo = -1, hi = 1e9;
    while(lo + 1 < hi){
        int m = (lo + hi) / 2;
        bool ok = false;
        for(int i = 0; i < 4; i++){
            if(check(m))
                ok = true;
            rotate();
        }
        if(ok)
            hi = m;
        else
            lo = m;
    }
    cout << hi << '\n';
}

signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...