# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
752884 | bgnbvnbv | The Kingdom of JOIOI (JOI17_joioi) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=4e6+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll n,m;
ll ans=inf;
ll a[2006][2006],b[2006][2006],mx=-inf,mn=inf;
bool check(ll mid)
{
ll mxr=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(b[i][j]<mx-mid) mxr=max(mxr,j);
}
for(int j=1;j<=mxr;j++)
{
if(b[i][j]-mn>mid) return false;
}
}
return true;
}
ll solve1()
{
ll low=0,high=mx-mn-1;
while(low<=high)
{
ll mid=low+high>>1;
if(check(mid)) high=mid-1;
else low=mid+1;
}
return low;
}
void rot()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
b[m-j+1][i]=a[i][j];
}
}
swap(m,n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) a[i][j]=b[i][j];
}
}
void solve()
{
cin >> n >> m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) cin >> a[i][j],b[i][j]=a[i][j],mx=max(mx,a[i][j]),mn=min(mn,a[i][j]);
}
ans=min(ans,solve1());
rot();
ans=min(ans,solve1());
rot();
ans=min(ans,solve1());
rot();
ans=min(ans,solve1());
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}