#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 11;
const int B = 800;
int a[1001][1001];
int mx[1001][1001];
bool vis[1001][1001];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int n, m;
bool in(int i, int j){
return 0 <= i && i < n && 0 <= j && j < m;
}
vector<pair<int, int>> dq[2001];
int32_t main(){
cin >> n >> m;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
mx[i][j] = a[i][j];
}
}
int _mx = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
_mx = max(_mx, a[i][j]);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(mx[i][j] >= _mx - n - m) dq[n + m + mx[i][j] - _mx].push_back({mx[i][j], i * m + j});
}
}
for(int k = n + m; k >= 0; k--){
for(auto t : dq[k]){
int i = t.se / m, j = t.se % m, val = t.fi;
if(mx[i][j] != val || vis[i][j]) continue; vis[i][j] = true;
for(int dir = 0; dir < 4; dir++){
if(in(i + dx[dir], j + dy[dir]) && mx[i + dx[dir]][j + dy[dir]] < mx[i][j] - 1){
mx[i + dx[dir]][j + dy[dir]] = mx[i][j] - 1;
int ni = i + dx[dir], nj = j + dy[dir];
dq[n + m + mx[i + dx[dir]][j + dy[dir]] - _mx].push_back({mx[i + dx[dir]][j + dy[dir]], ni * m + nj});
}
}
}
}
int ans = -1;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
ans = max(ans, mx[i][j] - a[i][j] - 1);
}
}
cout << ans << endl;
}
Compilation message
maxcomp.cpp: In function 'int32_t main()':
maxcomp.cpp:48:4: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
48 | if(mx[i][j] != val || vis[i][j]) continue; vis[i][j] = true;
| ^~
maxcomp.cpp:48:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
48 | if(mx[i][j] != val || vis[i][j]) continue; vis[i][j] = true;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
804 KB |
Output is correct |
10 |
Correct |
2 ms |
876 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
804 KB |
Output is correct |
13 |
Correct |
2 ms |
876 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
852 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
366 ms |
37320 KB |
Output is correct |
19 |
Correct |
363 ms |
43724 KB |
Output is correct |
20 |
Correct |
347 ms |
41696 KB |
Output is correct |
21 |
Correct |
384 ms |
45148 KB |
Output is correct |
22 |
Correct |
357 ms |
45220 KB |
Output is correct |
23 |
Correct |
362 ms |
45128 KB |
Output is correct |
24 |
Correct |
372 ms |
44732 KB |
Output is correct |