#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 = 1; 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |