#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 2005;
int a[MX][MX];
int H, W, mn = 1e9, mx = 0;
bool can(int x) {
// cek atas kanan
int lst = W - 1;
bool ok = 1;
for(int i = 0; i < H; i++) {
for(int j = 0; j <= lst; j++) {
if(a[i][j] > mn + x) {
lst = j - 1;
break;
}
}
for(int j = lst + 1; j < W; j++) {
ok &= a[i][j] >= mx - x;
}
}
if(ok) return 1;
ok = 1;
lst = 0;
for(int i = 0; i < H; i++) {
for(int j = W - 1; j >= lst; j--) {
if(a[i][j] > mn + x) {
lst = j + 1;
break;
}
}
for(int j = 0; j < lst; j++) {
ok &= a[i][j] >= mx - x;
}
}
if(ok) return 1;
ok = 1;
lst = W - 1;
for(int i = H - 1; i >= 0; i--) {
for(int j = 0; j <= lst; j++) {
if(a[i][j] > mn + x) {
lst = j - 1;
break;
}
}
for(int j = lst + 1; j < W; j++) {
ok &= a[i][j] >= mx - x;
}
}
if(ok) return 1;
ok = 1;
lst = 0;
for(int i = H - 1; i >= 0; i--) {
for(int j = W - 1; j >= lst; j--) {
if(a[i][j] > mn + x) {
lst = j + 1;
break;
}
}
for(int j = 0; j < lst; j++) {
ok &= a[i][j] >= mx - x;
}
}
return ok;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> H >> W;
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int l = 0, r = 1e9, ans = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if(can(mid)) {
r = mid - 1, ans = mid;
} else {
l = mid + 1;
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1276 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
7 ms |
1432 KB |
Output is correct |
19 |
Correct |
7 ms |
1492 KB |
Output is correct |
20 |
Correct |
6 ms |
1364 KB |
Output is correct |
21 |
Correct |
10 ms |
1620 KB |
Output is correct |
22 |
Correct |
9 ms |
1620 KB |
Output is correct |
23 |
Correct |
7 ms |
1620 KB |
Output is correct |
24 |
Correct |
6 ms |
1492 KB |
Output is correct |
25 |
Correct |
8 ms |
1588 KB |
Output is correct |
26 |
Correct |
9 ms |
1620 KB |
Output is correct |
27 |
Correct |
9 ms |
1608 KB |
Output is correct |
28 |
Correct |
8 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1276 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
7 ms |
1432 KB |
Output is correct |
19 |
Correct |
7 ms |
1492 KB |
Output is correct |
20 |
Correct |
6 ms |
1364 KB |
Output is correct |
21 |
Correct |
10 ms |
1620 KB |
Output is correct |
22 |
Correct |
9 ms |
1620 KB |
Output is correct |
23 |
Correct |
7 ms |
1620 KB |
Output is correct |
24 |
Correct |
6 ms |
1492 KB |
Output is correct |
25 |
Correct |
8 ms |
1588 KB |
Output is correct |
26 |
Correct |
9 ms |
1620 KB |
Output is correct |
27 |
Correct |
9 ms |
1608 KB |
Output is correct |
28 |
Correct |
8 ms |
1620 KB |
Output is correct |
29 |
Correct |
434 ms |
15600 KB |
Output is correct |
30 |
Correct |
476 ms |
16440 KB |
Output is correct |
31 |
Correct |
466 ms |
16476 KB |
Output is correct |
32 |
Correct |
485 ms |
16460 KB |
Output is correct |
33 |
Correct |
381 ms |
14628 KB |
Output is correct |
34 |
Correct |
466 ms |
16500 KB |
Output is correct |
35 |
Correct |
686 ms |
17180 KB |
Output is correct |
36 |
Correct |
591 ms |
16828 KB |
Output is correct |
37 |
Correct |
704 ms |
17136 KB |
Output is correct |