This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e3 + 16;
const ll Log2 = 19;
const ll inf = 1e13 + 7;
typedef pair<ll,ll> LL;
///Road to EF/M
ll n,m,a[N][N],b[N][N],ans = inf,mx,mn = inf;
ll resis[N],Inclu[N];
void LeftRight(){
for (ll i = 1;i <= n;i++)
for (ll j = 1;j <= m;j++) b[i][m - j + 1] = a[i][j];
for (ll i = 1;i <= n;i++)
for (ll j = 1;j <= m;j++) a[i][j] = b[i][j];
}
void out(){
for (ll i = 1;i <= n;i++){
for (ll j = 1;j <= m;j++) cout<<a[i][j]<<" ";
cout<<"\n";
}
exit(0);
}
bool chk(ll mid){
if (mx - mid > mn + mid) return 0;
for (ll i = 1;i <= m;i++) resis[i] = 0,Inclu[i] = inf;
for (ll i = 1;i <= n;i++)
for (ll j = 1;j <= m;j++){
if (a[i][j] > mn + mid) Inclu[j] = min(Inclu[j],i);
if (a[i][j] < mx - mid) resis[j] = max(resis[j],i);
}
//for (ll i = 1;i <= m;i++) cout<<Inclu[i]<<" "; exit(0);
ll now = inf;
for (ll i = 1;i <= m;i++){
now = min(now,Inclu[i]);
//cout<<now<<" x "<<resis[i]<<"\n";
if (resis[i] >= now) return 0;
}
return 1;
}
void oper(){
ll l,mid,h;
l = 0; h = mx - mn;
//cout<<chk(11); exit(0);
while(l <= h){
mid = (l + h)/2;
if (chk(mid)) h = mid - 1;
else l = mid + 1;
}
ans = min(ans,l);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "tst"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
}
cin>>n>>m;
for (ll i = 1;i <= n;i++)
for (ll j = 1;j <= m;j++) cin>>a[i][j],mx = max(mx,a[i][j]),mn = min(mn,a[i][j]);
oper();
LeftRight(); oper();
cout<<ans;
}
Compilation message (stderr)
joioi.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization "O2"
|
joioi.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
joioi.cpp: In function 'int main()':
joioi.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |