#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 1e3+5;
int v[N][N];
struct node {
int mx, mn, sz, val;
node(){
mx=INT_MIN, mn=INT_MAX, sz=0, val=INT_MIN;
};
} dp[N][N];
int main(){
int n, m;
scanf("%d%d", &n, &m);
auto is=[&](int a, int b) -> bool {
return (a>=1 and a<=n and b>=1 and b<=m);
};
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
scanf("%d", &v[i][j]);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j){
// 1-> up
if(is(i-1, j)){
if(umax(dp[i][j].val, max(dp[i-1][j].mx, v[i][j])-min(dp[i-1][j].mn, v[i][j])-dp[i-1][j].sz-1))
umax(dp[i][j].mx, max(dp[i-1][j].mx, v[i][j])), umin(dp[i][j].mn, min(dp[i-1][j].mn, v[i][j])), dp[i][j].sz=dp[i-1][j].sz+1;
}
// 2-> left
if(is(i, j-1)){
if(umax(dp[i][j].val, max(dp[i][j-1].mx, v[i][j])-min(dp[i][j-1].mn, v[i][j])-dp[i][j-1].sz-1))
umax(dp[i][j].mx, max(dp[i][j-1].mx, v[i][j])), umin(dp[i][j].mn, min(dp[i][j-1].mn, v[i][j])), dp[i][j].sz=dp[i][j-1].sz+1;
}
// 3-> start new
if(umax(dp[i][j].val, -1))
dp[i][j].mx=v[i][j], dp[i][j].mn=v[i][j], dp[i][j].sz=1;
}
int answer=INT_MIN;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
umax(answer, dp[i][j].val);
printf("%d\n", answer);
return 0;
}
Compilation message (stderr)
maxcomp.cpp: In function 'int main()':
maxcomp.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
maxcomp.cpp:33:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d", &v[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |