Submission #1150412

#TimeUsernameProblemLanguageResultExecution timeMemory
1150412AndrijaMThe Kingdom of JOIOI (JOI17_joioi)C++20
100 / 100
502 ms31908 KiB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\n'

const int maxn=2e3+10;

int n,m;
int a[maxn][maxn];
int mx=0,mn=1e18;

void RX()
{
    for(int i=0;i<n/2;i++)
    {
        for(int j=0;j<m;j++)
        {
            swap(a[i][j],a[n-i-1][j]);
        }
    }
}

void RY()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m/2;j++)
        {
            swap(a[i][j],a[i][m-j-1]);
        }
    }
}

bool check(int k)
{
    int kolona=-1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mx-a[i][j]>k)
            {
                kolona=max(kolona,j);
            }
        }
        for(int j=0;j<=kolona;j++)
        {
            if(a[i][j]-mn>k)
            {
                return false;
            }
        }
    }
    return true;
}


int f()
{
    int l=0;
    int r=1e9;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            r=mid;
        }
        else
        {
            l=mid+1;
        }
    }
    return r;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            mx=max(mx,a[i][j]);
            mn=min(mn,a[i][j]);
        }
    }
    int ans=f();
    RX();
    ans=min(ans,f());
    RY();
    ans=min(ans,f());
    RX();
    ans=min(ans,f());
    cout<<ans<<endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...