This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 2010;
int n,m;
int grid[maxn][maxn];
int mn=mod,mx;
int pos[maxn];
int ans = 0;
bool check(int dif) {
//mn on the top
int lst = n-1;
rep(i,0,m) {
int cur = -1;
while (cur+1<=lst and grid[cur+1][i]-mn<=dif) cur++;
pos[i] = cur;
lst = cur;
}
bool ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (mx-grid[i][j]>dif) ok=true;
}
if (!ok) return true;
lst = n-1;
for (int i=m-1;i>=0;i--) {
int cur = -1;
while (cur+1<=lst and grid[cur+1][i]-mn<=dif) cur++;
pos[i] = cur;
lst = cur;
}
ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (mx-grid[i][j]>dif) ok=true;
}
if (!ok) return true;
//mx on the top
lst = n-1;
rep(i,0,m) {
int cur = -1;
while (cur+1<=lst and mx-grid[cur+1][i]<=dif) cur++;
pos[i] = cur;
lst = cur;
}
ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (grid[i][j]-mn>dif) ok=true;
}
if (!ok) return true;
lst = n-1;
for (int i=m-1;i>=0;i--) {
int cur = -1;
while (cur+1<=lst and mx-grid[cur+1][i]<=dif) cur++;
pos[i] = cur;
lst = cur;
}
ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (grid[i][j]-mn>dif) ok=true;
}
return !ok;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
rep(i,0,n)
rep(j,0,m) cin>>grid[i][j],mn = min(mn,grid[i][j]),mx = max(mx,grid[i][j]);
int l = 0,r=1e9;
while (l<r) {
int mid=l+r>>1;
if (check(mid)) r=mid;
else l=mid+1;
}
cout<<l;
return 0;
}
Compilation message (stderr)
joioi.cpp: In function 'int main()':
joioi.cpp:97:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
97 | int mid=l+r>>1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |