This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
#define int ll
const int INF = 1e14 + 123;
ll dp[5][1200][1200], mn[5][1200][1200], a[1200][1200];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
auto relax_min = [](int &x, int y) {
if (x > y) {
x = y;
return true;
}
return false;
};
auto relax_max = [](int &x, int y) {
if (x < y) {
x = y;
return true;
}
return false;
};
for (int k = 0; k <= 4; k++) {
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= m + 1; j++) {
dp[k][i][j] = INF;
mn[k][i][j] = INF;
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
dp[0][i][j] = a[i][j] - i - j,
mn[0][i][j] = min({dp[0][i][j], mn[0][i - 1][j], mn[0][i][j - 1]});
for (int i = 1; i <= n; i++)
for (int j = m; j >= 1; j--)
dp[1][i][j] = a[i][j] - i + j,
mn[1][i][j] = min({dp[1][i][j], mn[1][i - 1][j], mn[1][i][j + 1]});
for (int i = n; i >= 1; i--)
for (int j = 1; j <= m; j++)
dp[2][i][j] = a[i][j] + i - j,
mn[2][i][j] = min({dp[2][i][j], mn[2][i + 1][j], mn[2][i][j - 1]});
for (int i = n; i >= 1; i--)
for (int j = m; j >= 1; j--)
dp[3][i][j] = a[i][j] + i + j,
mn[3][i][j] = min({dp[3][i][j], mn[3][i + 1][j], mn[3][i][j + 1]});
ll ans = -1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
ans = max(ans, dp[0][i][j] - mn[0][i][j] - 1);
ans = max(ans, dp[1][i][j] - mn[1][i][j] - 1);
ans = max(ans, dp[2][i][j] - mn[2][i][j] - 1);
ans = max(ans, dp[3][i][j] - mn[3][i][j] - 1);
}
}
cout << ans;
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message (stderr)
maxcomp.cpp: In function 'void solve()':
maxcomp.cpp:71:10: warning: variable 'relax_min' set but not used [-Wunused-but-set-variable]
71 | auto relax_min = [](int &x, int y) {
| ^~~~~~~~~
maxcomp.cpp:78:10: warning: variable 'relax_max' set but not used [-Wunused-but-set-variable]
78 | auto relax_max = [](int &x, int y) {
| ^~~~~~~~~
maxcomp.cpp: In function 'void setIO(const string&)':
maxcomp.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
maxcomp.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |