이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e9 + 1;
int n, m, mn = INF, mx = -INF;
vector<vector<int>> a;
void rotate() {
vector<vector<int>> b(m, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
b[j][n - 1 - i] = a[i][j];
}
}
a = b;
swap(n, m);
}
int minInCol(int i, int j) {
int ret = INF;
for (int k = n - 1; k >= i; k--) {
ret = min(ret, a[k][j]);
}
return ret;
}
int maxInRow(int i, int j) {
int ret = 0;
for (int k = 0; k <= j; k++) {
ret = max(ret, a[i][k]);
}
return ret;
}
pair<int, int> solve(int i, int j) {
if (i == n) return {-INF, INF};
if (j == -1) return {-INF, INF};
pair<int, int> p1 = solve(i + 1, j);
p1.first = max(p1.first, maxInRow(i, j));
pair<int, int> p2 = solve(i, j - 1);
p2.second = min(p2.second, minInCol(i, j));
return max(p1.first - mn, mx - p1.second) < max(p2.first - mn, mx - p2.second) ? p1 : p2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
a.resize(n, vector<int>(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]);
mx = max(mx, a[i][j]);
}
}
int ans = mx - mn;
for (int _ = 0; _ < 4; _++) {
pair<int, int> res = solve(0, m - 1);
ans = min(ans, max(res.first - mn, mx - res.second));
rotate();
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |