Submission #197275

#TimeUsernameProblemLanguageResultExecution timeMemory
197275Bagritsevich_StepanMaxcomp (info1cup18_maxcomp)C++14
100 / 100
161 ms20708 KiB
//#include "/Users/stdc++.h"
#include <bits/stdc++.h>
 
using namespace std;

#define mp make_pair
#define pb push_back
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pii pair < int, int >
#define fs first
#define sc second
#define getfiles ifstream cin("input.txt"); ofstream cout("output.txt");
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

typedef long long ll;
typedef long double ld;

const int INF = 2e9;
const ll BIG_INF = 1e18;
const ll MOD = 1e9 + 7;

const int maxn = 1e3 + 5;
int mat[maxn][maxn], pref[maxn][maxn];
int a[maxn][maxn], n, m, ans;

void rev(int i_k, int j_k) {
    if (i_k == -1 && j_k == -1)
        return;
    
    if (!(i_k == -1 && j_k == 1)) {
        for (int i = 0; i < n / 2; i++)
            for (int j = 0; j < m; j++)
                swap(mat[i][j], mat[n - i - 1][j]);
    }
    
    if (i_k == 1 && j_k == -1)
        return;
    
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m / 2; j++)
            swap(mat[i][j], mat[i][m - j - 1]);
}

void upd(int i_k, int j_k) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            mat[i][j] = a[i][j] + i_k * i + j_k * j;
    
    rev(i_k, j_k);
    
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            pref[i + 1][j + 1] = min(min(pref[i + 1][j], pref[i][j + 1]), mat[i][j]);
}

void upd_ans() {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            ans = max(ans, mat[i][j] - pref[i + 1][j + 1] - 1);
}

int main() {
    fast_io
    cin >> n >> m;
    
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= m; j++)
            pref[i][j] = INF;
    
    ans = -1;
    
    upd(1, 1);
    upd_ans();
    
    upd(1, -1);
    upd_ans();
    
    upd(-1, 1);
    upd_ans();
    
    upd(-1, -1);
    upd_ans();
    
    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...