Submission #574079

#TimeUsernameProblemLanguageResultExecution timeMemory
574079groshiThe Kingdom of JOIOI (JOI17_joioi)C++17
100 / 100
460 ms54844 KiB
#include<iostream>

using namespace std;
int h,w;
int t[2010][2010];
int minn=1e9,maxx=0;
bool lewy_min(int sre)
{
    int lewo=w;
    for(int i=1;i<=h;i++)
    {
        for(int j=lewo;j>=1;j--)
            if(maxx-t[i][j]>sre)
                lewo=j-1;
        for(int j=lewo+1;j<=w;j++)
            if(t[i][j]-minn>sre)
                return 0;
    }
    return 1;
}
bool lewy_maxx(int sre)
{
    int lewo=w;
    for(int i=1;i<=h;i++)
    {
        for(int j=lewo;j>=1;j--)
            if(t[i][j]-minn>sre)
                lewo=j-1;
        for(int j=lewo+1;j<=w;j++)
            if(maxx-t[i][j]>sre)
                return 0;
    }
    return 1;
}
bool prawo_min(int sre)
{
    int prawo=w;
    for(int i=h;i>=1;i--)
    {
        for(int j=prawo;j>=1;j--)
            if(maxx-t[i][j]>sre)
                prawo=j-1;
        for(int j=prawo+1;j<=w;j++)
            if(t[i][j]-minn>sre)
                return 0;
    }
    return 1;
}
bool prawo_maxx(int sre)
{
    int prawo=w;
    for(int i=h;i>=1;i--)
    {
        for(int j=prawo;j>=1;j--)
            if(t[i][j]-minn>sre)
                prawo=j-1;
        for(int j=prawo+1;j<=w;j++)
            if(maxx-t[i][j]>sre)
                return 0;
    }
    return 1;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    cin>>h>>w;
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
        {
            cin>>t[i][j];
            maxx=max(maxx,t[i][j]);
            minn=min(minn,t[i][j]);
        }
    }
    int pocz=0,kon=1e9+1,sre,ostd=0;
    while(pocz<kon)
    {
        sre=(pocz+kon)/2;
        if(lewy_min(sre) || lewy_maxx(sre) || prawo_min(sre) || prawo_maxx(sre))
        {
            kon=sre;
            ostd=sre;
        }
        else pocz=sre+1;
    }
    cout<<ostd;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...