#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define pii pair<int, int>
#define pip pair<int, pii>
#define f first
#define s second
#define pb push_back
#define endl '\n'
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#ifdef TOAD
#define IOS()
#else
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const ll maxn = 1e3+5;
const ll mod = 1e9+7;
const ll inf = 1ll<<60;
int n, m;
int arr[maxn][maxn];
int dis[maxn][maxn];
bool occ[maxn][maxn];
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
bool ok(int a, int b){
return a >= 1 && b >= 1 && a <= n && b <= m;
}
signed main(){
IOS();
cin>>n>>m;
priority_queue<pip, vector<pip>, greater<pip> > pq;
REP1(i, n){
REP1(j, m){
cin>>arr[i][j];
dis[i][j] = arr[i][j];
pq.push({arr[i][j], {i, j}});
}
}
while(SZ(pq)){
pip tp = pq.top(); pq.pop();
if (occ[tp.s.f][tp.s.s]) continue;
occ[tp.s.f][tp.s.s] = 1;
REP(j, 4){
pii nw = {tp.s.f + dx[j], tp.s.s + dy[j]};
if (ok(nw.f, nw.s) && dis[nw.f][nw.s] > dis[tp.s.f][tp.s.s] + 1){
dis[nw.f][nw.s] = dis[tp.s.f][tp.s.s] + 1;
pq.push({dis[nw.f][nw.s], nw});
}
}
}
int ans = -inf;
REP1(i, n){
REP1(j, m){
ans = max(ans, arr[i][j] - dis[i][j] - 1);
}
}
cout<<ans<<endl;
}
/*
2 3
2 4 3
5 7 5
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |