#include <bits/stdc++.h>
using namespace std;
const int N = 2000 + 3;
int n, m, a[N][N];
int mi = INT_MAX, mx = 0;
bool chk(int d) {
for (int i = 1, p = 0; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (a[i][j] + d < mx) p = max(p, j + 1);
}
for (int j = 1; j <= m; ++j) {
if (a[i][j] - d > mi && j < p) return 0;
}
}
return 1;
}
int calc() {
int l = 0, r = mx - mi - 1, ans = r + 1, mid;
while (l <= r) {
mid = (l + r) >> 1;
if (chk(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
return ans;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mi = min(mi, a[i][j]);
}
}
int ans = calc();
reverse(a + 1, a + n + 1);
ans = min(ans, calc());
for (int i = 1; i <= n; ++i)
reverse(a[i] + 1, a[i] + m + 1);
ans = min(ans, calc());
reverse(a + 1, a + n + 1);
ans = min(ans, calc());
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
272 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
272 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
1860 KB |
Output is correct |
17 |
Correct |
8 ms |
2100 KB |
Output is correct |
18 |
Correct |
7 ms |
2124 KB |
Output is correct |
19 |
Correct |
8 ms |
2124 KB |
Output is correct |
20 |
Correct |
7 ms |
1824 KB |
Output is correct |
21 |
Correct |
9 ms |
2228 KB |
Output is correct |
22 |
Correct |
11 ms |
2220 KB |
Output is correct |
23 |
Correct |
13 ms |
2244 KB |
Output is correct |
24 |
Correct |
9 ms |
1952 KB |
Output is correct |
25 |
Correct |
14 ms |
2180 KB |
Output is correct |
26 |
Correct |
11 ms |
2240 KB |
Output is correct |
27 |
Correct |
9 ms |
2244 KB |
Output is correct |
28 |
Correct |
9 ms |
2176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
272 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
1860 KB |
Output is correct |
17 |
Correct |
8 ms |
2100 KB |
Output is correct |
18 |
Correct |
7 ms |
2124 KB |
Output is correct |
19 |
Correct |
8 ms |
2124 KB |
Output is correct |
20 |
Correct |
7 ms |
1824 KB |
Output is correct |
21 |
Correct |
9 ms |
2228 KB |
Output is correct |
22 |
Correct |
11 ms |
2220 KB |
Output is correct |
23 |
Correct |
13 ms |
2244 KB |
Output is correct |
24 |
Correct |
9 ms |
1952 KB |
Output is correct |
25 |
Correct |
14 ms |
2180 KB |
Output is correct |
26 |
Correct |
11 ms |
2240 KB |
Output is correct |
27 |
Correct |
9 ms |
2244 KB |
Output is correct |
28 |
Correct |
9 ms |
2176 KB |
Output is correct |
29 |
Correct |
339 ms |
16228 KB |
Output is correct |
30 |
Correct |
384 ms |
16964 KB |
Output is correct |
31 |
Correct |
491 ms |
17056 KB |
Output is correct |
32 |
Correct |
360 ms |
16840 KB |
Output is correct |
33 |
Correct |
294 ms |
14944 KB |
Output is correct |
34 |
Correct |
420 ms |
16956 KB |
Output is correct |
35 |
Correct |
706 ms |
17860 KB |
Output is correct |
36 |
Correct |
651 ms |
17428 KB |
Output is correct |
37 |
Correct |
904 ms |
17928 KB |
Output is correct |