#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int, int>
#define ff first
#define ss second
int a[2011][2011], b[2010][2010];
int mn, mx;
int n, m;
bool check(int x){
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
int ok2=(mx - a[i][j] <= x), ok1=(a[i][j] - mn <= x);
if(!ok1 and !ok2) return 0;
if(ok1 and ok2){
b[i][j] = 0;
}
else if(ok1) b[i][j] = 1;
else if(ok2) b[i][j] = 2;
}
}
//cout<<x<<'\n';
//for(int i=1; i<=n; i++){
// for(int j=1; j<=m; j++){
// cout<<b[i][j];
// }
// cout<<'\n';
//}
int ls = 0;
for(int j=m; j>=1; j--){
for(int i=ls+1; i<=n; i++){
if(b[i][j] == 1){
ls = max(ls, i);
}
}
for(int i=1; i<=ls; i++){
if(b[i][j] == 2){
//cout<<a[i][j]<<'\n';
return 0 ;
}
}
}
return 1;
}
int solve(int N, int M){
n = N;
m = M;
int l=0, r=2e9 + 1;
while(l + 1 < r){
int mid=(l+r)/2;
if(check(mid)) r = mid;
else l = mid;
}
return r;
}
void rotate(){
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
b[m - j + 1][i] = a[i][j];
}
}
swap(n, m);
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
a[i][j] = b[i][j];
}
}
//for(int i=1; i<=n; i++){
// for(int j=1; j<=m; j++){
// cout<<a[i][j]<<' ';
// }
// cout<<'\n';
//}
}
void solve(){
cin>>n>>m;
mn = 1e9;
mx = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int ans = solve(n, m);
rotate();
ans = min(ans, solve(n, m));
rotate();
ans = min(ans, solve(n, m));
rotate();
ans = min(ans, solve(n, m));
cout<<ans<<'\n';
}
signed main(){
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2492 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2484 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2492 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2492 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2492 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2484 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2492 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2492 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
17 ms |
8796 KB |
Output is correct |
17 |
Correct |
22 ms |
8796 KB |
Output is correct |
18 |
Correct |
19 ms |
8984 KB |
Output is correct |
19 |
Correct |
21 ms |
8796 KB |
Output is correct |
20 |
Correct |
18 ms |
8960 KB |
Output is correct |
21 |
Correct |
23 ms |
9052 KB |
Output is correct |
22 |
Correct |
20 ms |
8872 KB |
Output is correct |
23 |
Correct |
24 ms |
9052 KB |
Output is correct |
24 |
Correct |
33 ms |
8984 KB |
Output is correct |
25 |
Correct |
27 ms |
9008 KB |
Output is correct |
26 |
Correct |
21 ms |
9100 KB |
Output is correct |
27 |
Correct |
24 ms |
9052 KB |
Output is correct |
28 |
Correct |
28 ms |
9104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2492 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2484 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2492 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2492 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
17 ms |
8796 KB |
Output is correct |
17 |
Correct |
22 ms |
8796 KB |
Output is correct |
18 |
Correct |
19 ms |
8984 KB |
Output is correct |
19 |
Correct |
21 ms |
8796 KB |
Output is correct |
20 |
Correct |
18 ms |
8960 KB |
Output is correct |
21 |
Correct |
23 ms |
9052 KB |
Output is correct |
22 |
Correct |
20 ms |
8872 KB |
Output is correct |
23 |
Correct |
24 ms |
9052 KB |
Output is correct |
24 |
Correct |
33 ms |
8984 KB |
Output is correct |
25 |
Correct |
27 ms |
9008 KB |
Output is correct |
26 |
Correct |
21 ms |
9100 KB |
Output is correct |
27 |
Correct |
24 ms |
9052 KB |
Output is correct |
28 |
Correct |
28 ms |
9104 KB |
Output is correct |
29 |
Correct |
2491 ms |
85560 KB |
Output is correct |
30 |
Correct |
2476 ms |
85000 KB |
Output is correct |
31 |
Correct |
2718 ms |
86568 KB |
Output is correct |
32 |
Correct |
2417 ms |
86440 KB |
Output is correct |
33 |
Correct |
1884 ms |
83240 KB |
Output is correct |
34 |
Correct |
2619 ms |
86600 KB |
Output is correct |
35 |
Correct |
2596 ms |
102200 KB |
Output is correct |
36 |
Correct |
1873 ms |
96980 KB |
Output is correct |
37 |
Correct |
2627 ms |
102304 KB |
Output is correct |