/*
- The optimal subset is just a path between two cells
- The answer is thus max(first cell - last cell - Manhattan distance)
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll grid[1001][1001], val[1001][1001][2], mx[1002][1002][2], mn[1002][1002][2];
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> grid[i][j];
val[i][j][0] = grid[i][j] - i - j + 1;
val[i][j][1] = grid[i][j] - m + j - i;
}
ll ans = 0;
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= m + 1; j++) {
mx[i][j][0] = mx[i][j][1] = INT_MIN;
mn[i][j][0] = mn[i][j][1] = INT_MAX;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
mx[i][j][0] = max({val[i][j][0], mx[i - 1][j][0], mx[i][j - 1][0]});
mn[i][j][0] = min({val[i][j][0], mn[i - 1][j][0], mn[i][j - 1][0]});
ans = max({ans, val[i][j][0] - mn[i][j][0], mx[i][j][0] - val[i][j][0]});
}
for (int i = 1; i <= n; i++)
for (int j = m; j; j--) {
mx[i][j][1] = max({val[i][j][1], mx[i - 1][j][1], mx[i][j + 1][1]});
mn[i][j][1] = min({val[i][j][1], mn[i - 1][j][1], mn[i][j + 1][1]});
ans = max({ans, val[i][j][1] - mn[i][j][1], mx[i][j][1] - val[i][j][1]});
}
cout << ans - 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |