#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e9 + 1;
int n, m, mn = INF, mx = -INF;
vector<vector<pair<int, int>>> dp;
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};
if (dp[i][j].first != INF) return dp[i][j];
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));
dp[i][j] = max(p1.first - mn, mx - p1.second) < max(p2.first - mn, mx - p2.second) ? p1 : p2;
return dp[i][j];
}
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; _++) {
dp.clear();
dp.resize(n, vector<pair<int, int>>(m, {INF, -INF}));
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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
34 ms |
980 KB |
Output is correct |
17 |
Correct |
47 ms |
1236 KB |
Output is correct |
18 |
Correct |
37 ms |
1224 KB |
Output is correct |
19 |
Correct |
38 ms |
1236 KB |
Output is correct |
20 |
Correct |
34 ms |
1236 KB |
Output is correct |
21 |
Correct |
37 ms |
1364 KB |
Output is correct |
22 |
Correct |
38 ms |
1492 KB |
Output is correct |
23 |
Correct |
38 ms |
1364 KB |
Output is correct |
24 |
Correct |
33 ms |
1364 KB |
Output is correct |
25 |
Correct |
38 ms |
1364 KB |
Output is correct |
26 |
Correct |
38 ms |
1364 KB |
Output is correct |
27 |
Correct |
38 ms |
1364 KB |
Output is correct |
28 |
Correct |
38 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
34 ms |
980 KB |
Output is correct |
17 |
Correct |
47 ms |
1236 KB |
Output is correct |
18 |
Correct |
37 ms |
1224 KB |
Output is correct |
19 |
Correct |
38 ms |
1236 KB |
Output is correct |
20 |
Correct |
34 ms |
1236 KB |
Output is correct |
21 |
Correct |
37 ms |
1364 KB |
Output is correct |
22 |
Correct |
38 ms |
1492 KB |
Output is correct |
23 |
Correct |
38 ms |
1364 KB |
Output is correct |
24 |
Correct |
33 ms |
1364 KB |
Output is correct |
25 |
Correct |
38 ms |
1364 KB |
Output is correct |
26 |
Correct |
38 ms |
1364 KB |
Output is correct |
27 |
Correct |
38 ms |
1364 KB |
Output is correct |
28 |
Correct |
38 ms |
1364 KB |
Output is correct |
29 |
Execution timed out |
4057 ms |
67332 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |