이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e3 + 2;
int h, w, a[maxN][maxN], mxA_i;
bool b[maxN][maxN];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> h >> w;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> a[i][j], mxA_i = max(mxA_i, a[i][j]);
auto small_check = [&](int k) {
int max_right = w;
int mn = int(1e9), mx = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < max_right; j++) if (b[i][j])
max_right = j;
for (int j = max_right; j < w; j++)
mn = min(mn, a[i][j]), mx = max(mx, a[i][j]);
}
return mx - mn <= k;
};
auto reverse_rows = [&]() {
for (int i = 0; i < h; i++)
for (int j = 0; j < w / 2; j++)
swap(a[i][j], a[i][w - j - 1]), swap(b[i][j], b[i][w - j - 1]);
};
auto reverse_cols = [&]() {
for (int i = 0; i < h / 2; i++)
for (int j = 0; j < w; j++)
swap(a[i][j], a[h - i - 1][j]), swap(b[i][j], b[h - i - 1][j]);
};
auto bin_func = [&](int k) {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
b[i][j] = (mxA_i - a[i][j] > k);
bool res = false;
reverse_rows(); res |= small_check(k);
reverse_cols(); res |= small_check(k);
reverse_rows(); res |= small_check(k);
reverse_cols(); res |= small_check(k);
return res;
};
int lo = 1, hi = int(1e9), ans = hi;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (bin_func(mid)) ans = mid, hi = mid - 1;
else lo = mid + 1;
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |