Submission #1100785

#TimeUsernameProblemLanguageResultExecution timeMemory
1100785Kirill22Maxcomp (info1cup18_maxcomp)C++17
100 / 100
397 ms29580 KiB
#include "bits/stdc++.h"

using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int> (m)), dp(n, vector<int> (m));
    using T = array<int, 3>;
    priority_queue<T> tmp;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
            dp[i][j] = a[i][j] - 1;
            tmp.push({dp[i][j], i, j});
        }
    }
    while (!tmp.empty()) {
        auto [value, x, y] = tmp.top();
        tmp.pop();
        if (dp[x][y] != value) {
            continue;
        }
        for (auto [x2, y2] : vector<pair<int, int>> {{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}}) {
            if (x2 >= 0 && y2 >= 0 && x2 < n && y2 < m && dp[x2][y2] < value - 1) {
                dp[x2][y2] = value - 1;
                tmp.push({dp[x2][y2], x2, y2});
            }
        }
    }
    int ans = -1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ans = max(ans, dp[i][j] - a[i][j]);
        }
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...