Submission #407186

#TimeUsernameProblemLanguageResultExecution timeMemory
407186FEDIKUSThe Kingdom of JOIOI (JOI17_joioi)C++17
100 / 100
1451 ms33224 KiB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,ll) sort(a.begin(),a.end(),greater<ll>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define up(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define vstart auto startt=chrono::system_clock::now()
#define vend auto endd=chrono::system_clock::now()
#define vvreme chrono::duration<double> vremee=endd-startt
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef string str;

const ll maxn=2010;
ll matra[maxn][maxn];

ll h,w;
ll maxi=LLONG_MIN;
ll mini=LLONG_MAX;

bool moze(ll raz){
    ll dokle=LLONG_MAX;
    //cout<<raz<<"\n";
    for(ll i=0;i<h;i++){
        for(ll j=0;j<w;j++){
            if(matra[i][j]<maxi-raz){
                dokle=min(dokle,j);
            }
        }
        for(ll j=0;j<w;j++){
            if(matra[i][j]>mini+raz && j>=dokle) return false;
        }
    }
    return true;
}

void kolona(){
    for(ll i=0;i<h/2;i++)
        for(ll j=0;j<w;j++) swap(matra[i][j],matra[h-i-1][j]);
}

void red(){
    for(ll i=0;i<h;i++)
        for(ll j=0;j<w/2;j++) swap(matra[i][j],matra[i][w-j-1]);
}

void solve(){
    cin>>h>>w;
    for(ll i=0;i<h;i++)
        for(ll j=0;j<w;j++){
            cin>>matra[i][j];
            maxi=max(maxi,matra[i][j]);
            mini=min(mini,matra[i][j]);
        }
    ll res=LLONG_MAX;
    auto nesto=[&](){
        ll l=0;
        ll r=1e9;
        ll naj=-1;
        while(l<=r){
            ll mid=l+(r-l)/2;
            if(moze(mid)){
                r=mid-1;
                naj=mid;
            }else l=mid+1;
        }
        res=min(res,naj);
    };
    nesto();
    kolona();
    nesto();
    red();
    nesto();
    kolona();
    nesto();
    cout<<res;
}

int main(){
    ios;
    ll 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...