#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;
pli s[maxN];
bool use[2006][2006];
ll h[2006],h1[2006],h2[2006],h3[2006];;
ll b[2006][2006];
bool cmp(const pli&x, const pli&y)
{
return b[x.fi][x.se]<b[y.fi][y.se];
}
ll solve1(ll n,ll m)
{
ll ans=inf;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
s[(i-1)*m+j]={i,j};
}
}
//cout << '\n';
sort(s+1,s+m*n+1,cmp);
ll low=1,high=m*n;
while(low<=high)
{
ll mid=low+high>>1;
for(int i=1;i<=n;i++) h[i]=0,h1[i]=0,h2[i]=m+1,h3[i]=m+1;
for(int i=1;i<=m*n;i++)
{
if(b[s[i].fi][s[i].se]<b[s[mid].fi][s[mid].se])
{
ll x=s[i].fi;
ll y=s[i].se;
h[x]=max(h[x],y);
h1[x]=max(h1[x],y);
h2[x]=min(h2[x],y);
h3[x]=min(h3[x],y);
}
}
for(int i=n-1;i>=1;i--)
{
h[i]=max(h[i],h[i+1]);
}
for(int i=2;i<=n;i++)
{
h1[i]=max(h1[i],h1[i-1]);
}
for(int i=2;i<=n;i++)
{
h2[i]=min(h2[i],h2[i-1]);
}
for(int i=n-1;i>=1;i--) h3[i]=min(h3[i],h3[i+1]);
ll mx=0,mn=inf;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=h[i];j++)
{
mx=max(mx,b[i][j]);
mn=min(mn,b[i][j]);
}
}
ll cc=mx-mn;
mx=0,mn=inf;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=h1[i];j++)
{
mx=max(mx,b[i][j]);
mn=min(mn,b[i][j]);
}
}
cc=min(cc,mx-mn);
mx=0,mn=inf;
for(int i=1;i<=n;i++)
{
for(int j=m;j>=h2[i];j--)
{
mx=max(mx,b[i][j]);
mn=min(mn,b[i][j]);
}
}
cc=min(cc,mx-mn);
mx=0,mn=inf;
for(int i=1;i<=n;i++)
{
for(int j=m;j>=h3[i];j--)
{
mx=max(mx,b[i][j]);
mn=min(mn,b[i][j]);
}
}
cc=min(cc,mx-mn);
if(cc<=b[s[m*n].fi][s[m*n].se]-b[s[mid].fi][s[mid].se]) low=mid+1;
else high=mid-1;
}
return b[s[m*n].fi][s[m*n].se]-b[s[high].fi][s[high].se];
}
ll n,m;
ll ans=inf;
ll a[2006][2006];
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];
}
ans=min(ans,solve1(n,m));
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
joioi.cpp: In function 'll solve1(ll, ll)':
joioi.cpp:36:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | ll mid=low+high>>1;
| ~~~^~~~~
joioi.cpp:23:8: warning: unused variable 'ans' [-Wunused-variable]
23 | ll ans=inf;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |