#include "bits/stdc++.h"
#include <climits>
using namespace std;
using ll = long long;
int h, w;
int a[2005][2005];
int minv, maxv;
int t[2005][2005];
int t1[3][2005][2005];
int t2[3][2005][2005];
int t3[3][2005][2005];
int t4[3][2005][2005];
bool check(int thr) {
// cout << "!!" << thr << endl;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
t[i][j] = 0;
if (a[i][j] <= minv + thr) {
t[i][j] += 1;
}
if (a[i][j] >= maxv - thr) {
t[i][j] += 2;
}
// cout << i << " " << j << " " << t[i][j] << endl;
if (t[i][j] == 0) return false;
}
}
for (int k = 1; k <= 2; k++) {
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
t1[k][i][j] = (t[i][j] == k);
t1[k][i][j] = max(t1[k][i][j], max(t1[k][i - 1][j], t1[k][i][j - 1]));
}
}
for (int i = 1; i <= h; i++) {
for (int j = w; j >= 1; j--) {
t2[k][i][j] = (t[i][j] == k);
t2[k][i][j] = max(t2[k][i][j], max(t2[k][i - 1][j], t2[k][i][j + 1]));
}
}
for (int i = h; i >= 1; i--) {
for (int j = 1; j <= w; j++) {
t3[k][i][j] = (t[i][j] == k);
t3[k][i][j] = max(t3[k][i][j], max(t3[k][i + 1][j], t3[k][i][j - 1]));
}
}
for (int i = h; i >= 1; i--) {
for (int j = w; j >= 1; j--) {
t4[k][i][j] = (t[i][j] == k);
t4[k][i][j] = max(t4[k][i][j], max(t4[k][i + 1][j], t4[k][i][j + 1]));
}
}
}
// 1 2
// 3 4
bool flag1 = 1, flag2 = 1, flag3 = 1, flag4 = 1;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (t1[1][i][j] == 1 && t4[2][i][j] == 1) flag1 = 0;
if (t2[1][i][j] == 1 && t3[2][i][j] == 1) flag2 = 0;
if (t3[1][i][j] == 1 && t2[2][i][j] == 1) flag3 = 0;
if (t4[1][i][j] == 1 && t1[2][i][j] == 1) flag4 = 0;
}
}
return flag1 || flag2 || flag3 || flag4;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> h >> w;
minv = INT_MAX;
maxv = INT_MIN;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> a[i][j];
minv = min(minv, a[i][j]);
maxv = max(maxv, a[i][j]);
}
}
if (minv == maxv) {
cout << 0 << endl;
return 0;
}
int l = 0, r = maxv - minv;
while (l <= r) {
int d = (l + r) / 2;
if (check(d)) {
r = d - 1;
}
else {
l = d + 1;
}
}
cout << l << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
12 ms |
9848 KB |
Output is correct |
17 |
Correct |
44 ms |
10232 KB |
Output is correct |
18 |
Correct |
16 ms |
10200 KB |
Output is correct |
19 |
Correct |
38 ms |
10236 KB |
Output is correct |
20 |
Correct |
32 ms |
9044 KB |
Output is correct |
21 |
Correct |
50 ms |
10348 KB |
Output is correct |
22 |
Correct |
22 ms |
10196 KB |
Output is correct |
23 |
Correct |
49 ms |
10324 KB |
Output is correct |
24 |
Correct |
18 ms |
9132 KB |
Output is correct |
25 |
Correct |
56 ms |
10324 KB |
Output is correct |
26 |
Correct |
52 ms |
10308 KB |
Output is correct |
27 |
Correct |
50 ms |
10324 KB |
Output is correct |
28 |
Correct |
54 ms |
10348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
12 ms |
9848 KB |
Output is correct |
17 |
Correct |
44 ms |
10232 KB |
Output is correct |
18 |
Correct |
16 ms |
10200 KB |
Output is correct |
19 |
Correct |
38 ms |
10236 KB |
Output is correct |
20 |
Correct |
32 ms |
9044 KB |
Output is correct |
21 |
Correct |
50 ms |
10348 KB |
Output is correct |
22 |
Correct |
22 ms |
10196 KB |
Output is correct |
23 |
Correct |
49 ms |
10324 KB |
Output is correct |
24 |
Correct |
18 ms |
9132 KB |
Output is correct |
25 |
Correct |
56 ms |
10324 KB |
Output is correct |
26 |
Correct |
52 ms |
10308 KB |
Output is correct |
27 |
Correct |
50 ms |
10324 KB |
Output is correct |
28 |
Correct |
54 ms |
10348 KB |
Output is correct |
29 |
Correct |
2583 ms |
171532 KB |
Output is correct |
30 |
Correct |
2379 ms |
178864 KB |
Output is correct |
31 |
Correct |
2798 ms |
180292 KB |
Output is correct |
32 |
Correct |
2674 ms |
180356 KB |
Output is correct |
33 |
Correct |
986 ms |
158144 KB |
Output is correct |
34 |
Correct |
2738 ms |
180508 KB |
Output is correct |
35 |
Execution timed out |
4102 ms |
196024 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |