#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e3 + 2, MOD = (int)1e9 + 7;
int n,m,a[N][N],b[N][N];
void rot(){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
a[m - j + 1][i] = b[i][j];
}
}
swap(n,m);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
b[i][j] = a[i][j];
}
}
}
int mn = 1e9;
int it[N];
bool check(int mid){
int l = mn,r = l + mid;
it[0] = m;
for(int i = 1;i <= n;i++){
it[i] = 0;
for(int j = 1;j <= it[i - 1] && a[i][j] >= l && a[i][j] <= r;j++){
it[i] = j;
}
}
int mn = 1e9,mx = -1e9;
for(int i = 1;i <= n;i++){
for(int j = it[i] + 1;j <= m;j++){
mn = min(mn,a[i][j]);
mx = max(mx,a[i][j]);
}
}
return (mx - mn <= mid);
}
bool ok(int mid){
bool res = false;
for(int i = 0;i < 4;i++){
if(check(mid)){
res = 1;
}
rot();
}
return res;
}
void test(){
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];
mn = min(mn,a[i][j]);
}
}
int l = -1,r = 1e9 + 1;
while(r - l > 1){
int mid = (l + r) >> 1;
if(ok(mid)){
r = mid;
}else{
l = mid;
}
// cout << mid << ' ' << ok(mid) << ' ' << check(mid) << '\n';
}
cout << r << '\n';
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6744 KB |
Output is correct |
16 |
Correct |
8 ms |
6492 KB |
Output is correct |
17 |
Correct |
9 ms |
6492 KB |
Output is correct |
18 |
Correct |
9 ms |
6728 KB |
Output is correct |
19 |
Correct |
9 ms |
6744 KB |
Output is correct |
20 |
Correct |
11 ms |
6492 KB |
Output is correct |
21 |
Correct |
10 ms |
6732 KB |
Output is correct |
22 |
Correct |
9 ms |
6488 KB |
Output is correct |
23 |
Correct |
10 ms |
6492 KB |
Output is correct |
24 |
Correct |
8 ms |
6492 KB |
Output is correct |
25 |
Correct |
10 ms |
6488 KB |
Output is correct |
26 |
Correct |
10 ms |
6492 KB |
Output is correct |
27 |
Correct |
10 ms |
6492 KB |
Output is correct |
28 |
Correct |
9 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6744 KB |
Output is correct |
16 |
Correct |
8 ms |
6492 KB |
Output is correct |
17 |
Correct |
9 ms |
6492 KB |
Output is correct |
18 |
Correct |
9 ms |
6728 KB |
Output is correct |
19 |
Correct |
9 ms |
6744 KB |
Output is correct |
20 |
Correct |
11 ms |
6492 KB |
Output is correct |
21 |
Correct |
10 ms |
6732 KB |
Output is correct |
22 |
Correct |
9 ms |
6488 KB |
Output is correct |
23 |
Correct |
10 ms |
6492 KB |
Output is correct |
24 |
Correct |
8 ms |
6492 KB |
Output is correct |
25 |
Correct |
10 ms |
6488 KB |
Output is correct |
26 |
Correct |
10 ms |
6492 KB |
Output is correct |
27 |
Correct |
10 ms |
6492 KB |
Output is correct |
28 |
Correct |
9 ms |
6492 KB |
Output is correct |
29 |
Correct |
874 ms |
31804 KB |
Output is correct |
30 |
Correct |
856 ms |
53484 KB |
Output is correct |
31 |
Correct |
863 ms |
55120 KB |
Output is correct |
32 |
Correct |
923 ms |
54864 KB |
Output is correct |
33 |
Correct |
813 ms |
51764 KB |
Output is correct |
34 |
Correct |
841 ms |
55120 KB |
Output is correct |
35 |
Correct |
1000 ms |
70480 KB |
Output is correct |
36 |
Correct |
910 ms |
65428 KB |
Output is correct |
37 |
Correct |
1062 ms |
70996 KB |
Output is correct |