#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define endl "\n"
#define fr first
#define sc second
#define sz size()
#define ins insert
#define bc back()
#define str string
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e18+7;
const ll mod=1e9;
const ll N=2e3+7;
const ld eps=1e-9;
ll a[N][N],mx=0,mn=inf;
ll n,m;
ll i,j;
bool check(ll mid){
ll lim=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(a[i][j]<mx-mid)lim=max(lim,j+1);
}
for(j=1;j<lim;j++){
if(a[i][j]>mn+mid)return false;
}
}
return true;
}
void flip1(){
for(j=1;j<=m;++j)
for(i=1;i<=n/2;++i)
swap(a[n-i+1][j],a[i][j]);
}
void flip2(){
for(i=1;i<=n;++i)
for(j=1;j<=m/2;j++)
swap(a[i][m-j+1],a[i][j]);
}
ll bin(){
ll l=0,r=mx-mn;
while(r-l>1){
ll mid=(l+r)/2;
if(check(mid))r=mid;
else l=mid;
}
return r;
}
void solve(){
cin>>n>>m;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>a[i][j];
mx=max(mx,a[i][j]);
mn=min(mn,a[i][j]);
}
}
if(mn==mx){
cout<<0<<endl;
return;
}
ll ans=bin();
flip1();
ans=min(ans,bin());
flip2();
ans=min(ans,bin());
flip1();
ans=min(ans,bin());
cout<<ans<<endl;
}
signed main(){
start();
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
4 ms |
4700 KB |
Output is correct |
18 |
Correct |
4 ms |
4696 KB |
Output is correct |
19 |
Correct |
5 ms |
4696 KB |
Output is correct |
20 |
Correct |
4 ms |
4700 KB |
Output is correct |
21 |
Correct |
8 ms |
4828 KB |
Output is correct |
22 |
Correct |
6 ms |
4956 KB |
Output is correct |
23 |
Correct |
7 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4972 KB |
Output is correct |
25 |
Correct |
8 ms |
4956 KB |
Output is correct |
26 |
Correct |
6 ms |
4956 KB |
Output is correct |
27 |
Correct |
6 ms |
5128 KB |
Output is correct |
28 |
Correct |
7 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
4 ms |
4700 KB |
Output is correct |
18 |
Correct |
4 ms |
4696 KB |
Output is correct |
19 |
Correct |
5 ms |
4696 KB |
Output is correct |
20 |
Correct |
4 ms |
4700 KB |
Output is correct |
21 |
Correct |
8 ms |
4828 KB |
Output is correct |
22 |
Correct |
6 ms |
4956 KB |
Output is correct |
23 |
Correct |
7 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4972 KB |
Output is correct |
25 |
Correct |
8 ms |
4956 KB |
Output is correct |
26 |
Correct |
6 ms |
4956 KB |
Output is correct |
27 |
Correct |
6 ms |
5128 KB |
Output is correct |
28 |
Correct |
7 ms |
5212 KB |
Output is correct |
29 |
Correct |
276 ms |
53332 KB |
Output is correct |
30 |
Correct |
289 ms |
53328 KB |
Output is correct |
31 |
Correct |
342 ms |
54744 KB |
Output is correct |
32 |
Correct |
283 ms |
54880 KB |
Output is correct |
33 |
Correct |
263 ms |
49388 KB |
Output is correct |
34 |
Correct |
358 ms |
55000 KB |
Output is correct |
35 |
Correct |
511 ms |
70632 KB |
Output is correct |
36 |
Correct |
511 ms |
65608 KB |
Output is correct |
37 |
Correct |
608 ms |
70772 KB |
Output is correct |