#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int,int>
#define pip pair<int,pii>
#define ff first
#define ss second
using namespace std;
const int MAXN = 1000;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
vector <vector<int>> arr(n);
vector <pip> ord;
for (int i = 0; i < n; i++) {
arr[i].resize(m);
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
ord.pb(pip(arr[i][j], pii(i, j)));
}
}
sort(ord.begin(), ord.end(), greater<pip>());
int ans = -1, cnt = 0;
queue <pii> bfs;
vector <vector<bool>> vis(n);
vector <vector<int>> dis(n), mmin(n);
for (int j = 0; j < n; j++)
vis[j].resize(m), dis[j].resize(m), mmin[j].resize(m);
for (int i = 0; i < n * m; i++) {
if (ord[i].ff + 100 < ord[0].ff) break;
if (++cnt > 50) break;
for (int a = 0; a < n; a++)
for (int b = 0; b < m; b++)
vis[a][b] = 0;
bfs.push(ord[i].ss);
vis[ord[i].ss.ff][ord[i].ss.ss] = 1;
dis[ord[i].ss.ff][ord[i].ss.ss] = 1;
mmin[ord[i].ss.ff][ord[i].ss.ss] = ord[i].ff;
int val = ord[i].ff;
while (i+1 < n * m && ord[i+1].ff == ord[i].ff) {
bfs.push(ord[++i].ss);
vis[ord[i].ss.ff][ord[i].ss.ss] = 1;
dis[ord[i].ss.ff][ord[i].ss.ss] = 1;
mmin[ord[i].ss.ff][ord[i].ss.ss] = ord[i].ff;
}
vector <int> dx = {0, 1, -1, 0}, dy = {1, 0, 0, -1};
while (bfs.size()) {
pii w = bfs.front();
bfs.pop();
for (int j = 0; j < 4; j++) {
int x = w.ff + dx[j], y = w.ss + dy[j];
if (x >= n || x < 0 || y >= m || y < 0) continue;
if (!vis[x][y] && arr[x][y] < arr[ord[i].ss.ff][ord[i].ss.ss]) {
vis[x][y] = 1;
dis[x][y] = dis[w.ff][w.ss] + 1;
mmin[x][y] = min(arr[x][y], mmin[w.ff][w.ss]);
ans = max(ans, val - mmin[x][y] - dis[x][y]);
bfs.push(pii(x, y));
}
}
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
348 ms |
24160 KB |
Output is correct |
19 |
Execution timed out |
573 ms |
24168 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |