#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <string>
#include <algorithm>
#include <tuple>
#include <cassert>
using namespace std;
#define x first
#define y second
#define int long long
const int maxn = 1e3 + 10;
int mat[maxn][maxn];
int opt[maxn][maxn];
vector<pair<int, int>> dir = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int n, m;
bool check(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < m;
}
signed main() {
#ifdef LC
assert(freopen("input.txt", "r", stdin));
#endif
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
int ans = -1;
set<pair<pair<int, int>, pair<int, int>>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> mat[i][j];
opt[i][j] = -1;
q.insert({{mat[i][j], -1}, {i, j}});
}
}
while (!q.empty()) {
auto v = *q.rbegin();
q.erase(v);
int x = v.y.x;
int y = v.y.y;
for (auto el : dir) {
int x2 = el.x + x;
int y2 = el.y + y;
if (!check(x2, y2) || mat[x2][y2] > mat[x][y])
continue;
//cout << x2 << ' ' << y2 << endl;
if (opt[x2][y2] < opt[x][y] + mat[x][y] - mat[x2][y2] - 1) {
q.erase({{mat[x2][y2], opt[x2][y2]}, {x2, y2}});
opt[x2][y2] = max(opt[x2][y2], opt[x][y] + mat[x][y] - mat[x2][y2] - 1);
q.insert({{mat[x2][y2], opt[x2][y2]}, {x2, y2}});
ans = max(ans, opt[x2][y2]);
}
}
}
assert(ans != -1);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |