#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)(x).size()
using ll = long long;
const int mod = 1e9+7;
int n, m, mx = 0, mn = (1<<30);
vector<vector<int>> v;
bool chk(int x) {
int last = -1;
for (int i = 0; i < n; ++i) {
int cur = -1;
bool f = false;
for (int j = 0; j < m; ++j) {
if (v[i][j] < mx - x) {
if (v[i][j] > mx + x or f) return false;
cur = j;
} else if (v[i][j] > mn + x) {
if (j <= last) return false;
f = true;
}
}
last = max(last, cur);
}
return true;
}
int go() {
int lo = 0, hi = mx - mn;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (chk(mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
void solve(int tc) {
cin >> n >> m;
v.resize(n, vector<int>(m));
for (auto &x : v) for (auto &y : x) {
cin >> y;
mx = max(mx, y);
mn = min(mn, y);
}
auto a = v;
int ans = mx - mn;
ans = min(ans, go());
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
v[i][j] = a[i][m-j-1];
ans = min(ans, go());
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
v[i][j] = a[n-i-1][j];
ans = min(ans, go());
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
v[i][j] = a[n-i-1][m-j-1];
ans = min(ans, go());
cout << ans << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; ++i) solve(i);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |