#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
7 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
320 KB |
Output is correct |
6 |
Correct |
2 ms |
316 KB |
Output is correct |
7 |
Correct |
7 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
316 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
212 KB |
Output is correct |
11 |
Correct |
8 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
212 KB |
Output is correct |
13 |
Correct |
7 ms |
320 KB |
Output is correct |
14 |
Correct |
8 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
7 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
320 KB |
Output is correct |
6 |
Correct |
2 ms |
316 KB |
Output is correct |
7 |
Correct |
7 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
316 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
212 KB |
Output is correct |
11 |
Correct |
8 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
212 KB |
Output is correct |
13 |
Correct |
7 ms |
320 KB |
Output is correct |
14 |
Correct |
8 ms |
212 KB |
Output is correct |
15 |
Execution timed out |
4054 ms |
340 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
7 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
320 KB |
Output is correct |
6 |
Correct |
2 ms |
316 KB |
Output is correct |
7 |
Correct |
7 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
316 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
212 KB |
Output is correct |
11 |
Correct |
8 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
212 KB |
Output is correct |
13 |
Correct |
7 ms |
320 KB |
Output is correct |
14 |
Correct |
8 ms |
212 KB |
Output is correct |
15 |
Execution timed out |
4054 ms |
340 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |