// JOI chứa min, IOI chứa max
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2000 + 3, inf = 1e9 + 7;
int h, w;
int a[N][N], mi = inf, ma;
bool check1(int diff) { // trái trên
for (int i = 1, p(w); i <= h; ++i) {
for (int j = p; j; --j)
if (a[i][j] - mi > diff) p = min(p, j - 1); // [1..p] [p+1..w]
for (int j = p + 1; j <= w; ++j)
if (ma - a[i][j] > diff) return false;
}
return true;
}
bool check2(int diff) { // trái dưới
for (int i = h, p(w); i; --i) {
for (int j = p; j; --j)
if (a[i][j] - mi > diff) p = min(p, j - 1); // [1..p] [p+1..w]
for (int j = p + 1; j <= w; ++j)
if (ma - a[i][j] > diff) return false;
}
return true;
}
bool check3(int diff) { // phải trên
for (int i = 1, p(w); i <= h; ++i) {
for (int j = p; j; --j)
if (ma - a[i][j] > diff) p = min(p, j - 1); // [1..p] [p+1..w]
for (int j = p + 1; j <= w; ++j)
if (a[i][j] - mi > diff) return false;
}
return true;
}
bool check4(int diff) { // phải dưới
for (int i = h, p(w); i; --i) {
for (int j = p; j; --j)
if (ma - a[i][j] > diff) p = min(p, j - 1); // [1..p] [p+1..w]
for (int j = p + 1; j <= w; ++j)
if (a[i][j] - mi > diff) return false;
}
return true;
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> h >> w;
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j) {
cin >> a[i][j];
mi = min(mi, a[i][j]);
ma = max(ma, a[i][j]);
}
int lo(0), hi(ma - mi - 1), m;
while (lo <= hi) {
m = (lo + hi) >> 1;
if (check1(m) || check2(m) || check3(m) || check4(m))
hi = m - 1;
else lo = m + 1;
}
cout << hi + 1 << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
1228 KB |
Output is correct |
17 |
Correct |
6 ms |
1484 KB |
Output is correct |
18 |
Correct |
4 ms |
1496 KB |
Output is correct |
19 |
Correct |
4 ms |
1484 KB |
Output is correct |
20 |
Correct |
4 ms |
1356 KB |
Output is correct |
21 |
Correct |
6 ms |
1604 KB |
Output is correct |
22 |
Correct |
5 ms |
1604 KB |
Output is correct |
23 |
Correct |
5 ms |
1604 KB |
Output is correct |
24 |
Correct |
5 ms |
1484 KB |
Output is correct |
25 |
Correct |
6 ms |
1652 KB |
Output is correct |
26 |
Correct |
5 ms |
1604 KB |
Output is correct |
27 |
Correct |
6 ms |
1612 KB |
Output is correct |
28 |
Correct |
6 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
1228 KB |
Output is correct |
17 |
Correct |
6 ms |
1484 KB |
Output is correct |
18 |
Correct |
4 ms |
1496 KB |
Output is correct |
19 |
Correct |
4 ms |
1484 KB |
Output is correct |
20 |
Correct |
4 ms |
1356 KB |
Output is correct |
21 |
Correct |
6 ms |
1604 KB |
Output is correct |
22 |
Correct |
5 ms |
1604 KB |
Output is correct |
23 |
Correct |
5 ms |
1604 KB |
Output is correct |
24 |
Correct |
5 ms |
1484 KB |
Output is correct |
25 |
Correct |
6 ms |
1652 KB |
Output is correct |
26 |
Correct |
5 ms |
1604 KB |
Output is correct |
27 |
Correct |
6 ms |
1612 KB |
Output is correct |
28 |
Correct |
6 ms |
1536 KB |
Output is correct |
29 |
Correct |
289 ms |
37260 KB |
Output is correct |
30 |
Correct |
299 ms |
37456 KB |
Output is correct |
31 |
Correct |
306 ms |
39104 KB |
Output is correct |
32 |
Correct |
317 ms |
39112 KB |
Output is correct |
33 |
Correct |
264 ms |
34136 KB |
Output is correct |
34 |
Correct |
315 ms |
39192 KB |
Output is correct |
35 |
Correct |
429 ms |
54888 KB |
Output is correct |
36 |
Correct |
387 ms |
49580 KB |
Output is correct |
37 |
Correct |
470 ms |
54808 KB |
Output is correct |