#include <bits/stdc++.h>
using namespace std;
const int N = 2019;
int H, W;
int A[N][N];
int gmax, gmin;
void flipRow() { for (int i = 0; i < H/2; i++) for (int j = 0; j < W; j++) swap(A[i][j], A[H-i-1][j]); }
void flipCol() { for (int i = 0; i < H; i++) for (int j = 0; j < W/2; j++) swap(A[i][j], A[i][W-j-1]); }
bool check(int t) {
int sp = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) if (A[i][j] < gmax-t) sp = max(sp,j+1);
for (int j = 0; j < W; j++) if (gmin+t < A[i][j]) if (j < sp) return false;
}
return true;
}
int solve() {
int l = 0, r = gmax-gmin;
while (l < r) {
int mid = (l+r)/2;
if (check(mid)) r = mid;
else l = mid+1;
}
return l;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> H >> W;
for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> A[i][j];
gmin = gmax = A[0][0];
for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
gmin = min(gmin,A[i][j]);
gmax = max(gmax,A[i][j]);
}
int ans = solve();
flipRow();
ans = min(ans,solve());
flipCol();
ans = min(ans,solve());
flipRow();
ans = min(ans,solve());
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
1272 KB |
Output is correct |
17 |
Correct |
9 ms |
1500 KB |
Output is correct |
18 |
Correct |
9 ms |
1528 KB |
Output is correct |
19 |
Correct |
12 ms |
1528 KB |
Output is correct |
20 |
Correct |
10 ms |
1400 KB |
Output is correct |
21 |
Correct |
12 ms |
1656 KB |
Output is correct |
22 |
Correct |
13 ms |
1656 KB |
Output is correct |
23 |
Correct |
15 ms |
1656 KB |
Output is correct |
24 |
Correct |
12 ms |
1528 KB |
Output is correct |
25 |
Correct |
18 ms |
1656 KB |
Output is correct |
26 |
Correct |
14 ms |
1656 KB |
Output is correct |
27 |
Correct |
12 ms |
1656 KB |
Output is correct |
28 |
Correct |
12 ms |
1656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
1272 KB |
Output is correct |
17 |
Correct |
9 ms |
1500 KB |
Output is correct |
18 |
Correct |
9 ms |
1528 KB |
Output is correct |
19 |
Correct |
12 ms |
1528 KB |
Output is correct |
20 |
Correct |
10 ms |
1400 KB |
Output is correct |
21 |
Correct |
12 ms |
1656 KB |
Output is correct |
22 |
Correct |
13 ms |
1656 KB |
Output is correct |
23 |
Correct |
15 ms |
1656 KB |
Output is correct |
24 |
Correct |
12 ms |
1528 KB |
Output is correct |
25 |
Correct |
18 ms |
1656 KB |
Output is correct |
26 |
Correct |
14 ms |
1656 KB |
Output is correct |
27 |
Correct |
12 ms |
1656 KB |
Output is correct |
28 |
Correct |
12 ms |
1656 KB |
Output is correct |
29 |
Correct |
516 ms |
37448 KB |
Output is correct |
30 |
Correct |
549 ms |
37804 KB |
Output is correct |
31 |
Correct |
671 ms |
39340 KB |
Output is correct |
32 |
Correct |
485 ms |
39288 KB |
Output is correct |
33 |
Correct |
429 ms |
34168 KB |
Output is correct |
34 |
Correct |
638 ms |
39420 KB |
Output is correct |
35 |
Correct |
960 ms |
54876 KB |
Output is correct |
36 |
Correct |
833 ms |
49868 KB |
Output is correct |
37 |
Correct |
1188 ms |
54976 KB |
Output is correct |