#include<bits/stdc++.h>
using namespace std;
#define int long long
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
const int N = 2007, INF = 1e9 + 7;
int n, m, mn = INF, a[N][N], r[N];
int suffmn[N][N], suffmx[N][N];
bool check_(int mid) {
int mx = mn + mid;
for (int i = 0; i < n; ++i) { r[i] = 0; while (r[i] < m && a[i][r[i]] <= mx) ++r[i]; }
for (int i = 0; i < n; ++i) {
int MN = suffmn[i][r[i]], MX = suffmx[i][r[i]];
int c = r[i];
for (int ii = i - 1; ii >= 0; --ii) { c = min(c, r[ii]); MN = min(MN, suffmn[ii][c]); MX = max(MX, suffmx[ii][c]); }
c = r[i];
for (int ii = i + 1; ii < n; ++ii) { c = min(c, r[ii]); MN = min(MN, suffmn[ii][c]); MX = max(MX, suffmx[ii][c]); }
if (MX - MN <= mid) return 1;
}
return 0;
}
void kek() {
for (int i = 0; i < n; ++i) for (int j = 0; j < m / 2; ++j) swap(a[i][j], a[i][m - 1 - j]);
}
bool check(int mid) {
if (check_(mid)) return 1;
kek(); bool ans = check_(mid); kek();
return ans;
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> m;
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { cin >> a[i][j]; mn = min(mn, a[i][j]); }
for (int i = 0; i < n; ++i) {
suffmn[i][m] = INF; for (int j = m - 1; j >= 0; --j) suffmn[i][j] = min(suffmn[i][j + 1], a[i][j]);
suffmx[i][m] = -INF; for (int j = m - 1; j >= 0; --j) suffmx[i][j] = max(suffmx[i][j + 1], a[i][j]);
}
int l = -1, r = INF;
while (l < r - 1) { int mid = (l + r) >> 1; if (check(mid)) r = mid; else l = mid; }
cout << r << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |