This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define ll long long
#define ii pair<int,int>
#define iii pair<int,ii>
#define sz(x) ((int) x.size())
#define orta ((bas+son)>>1)
#define all(x) x.begin(),x.end()
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define pw(x) (1<<(x))
#define inf 2000000000
#define MOD 1000000007
#define N 1005
#define MAX 10000006
#define LOG 22
using namespace std;
int n,m,a[N][N],p1[N][N];
int ans=-inf;
int add[4][2]={-1,-1,-1,1,1,-1,1,1};
void check(int cur) {
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
umax(ans,a[i][j]+p1[i][j]+add[cur][0]*i+add[cur][1]*j-1);
}
}
}
int main() {
// freopen("input.txt","r",stdin);
scanf("%d %d",&n,&m);
for(int i=0;i<=n+1;i++) for(int j=0;j<=m+1;j++) p1[i][j]=-inf;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
p1[i][j]=-inf;
umax(p1[i][j],max(p1[i-1][j],p1[i][j-1]));
umax(p1[i][j],-a[i][j]+i+j);
}
}
check(0);
//printf("Cur ans-->%d\n",ans);
for(int i=1;i<=n;i++) {
for(int j=m;j>=1;j--) {
p1[i][j]=-inf;
umax(p1[i][j],max(p1[i-1][j],p1[i][j+1]));
umax(p1[i][j],-a[i][j]+i-j);
}
}
check(1);
// printf("Cur ans-->%d\n",ans);
for(int i=n;i>=1;i--) {
for(int j=1;j<=m;j++) {
p1[i][j]=-inf;
umax(p1[i][j],max(p1[i+1][j],p1[i][j-1]));
umax(p1[i][j],-a[i][j]-i+j);
}
}
check(2);
// printf("Cur ans-->%d\n",ans);
for(int i=n;i>=1;i--) {
for(int j=m;j>=1;j--) {
p1[i][j]=-inf;
umax(p1[i][j],max(p1[i+1][j],p1[i][j+1]));
umax(p1[i][j],-a[i][j]-i-j);
}
}
check(3);
// printf("Cur ans-->%d\n",ans);
printf("%d",ans);
}
Compilation message (stderr)
maxcomp.cpp: In function 'int main()':
maxcomp.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
maxcomp.cpp:54:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[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... |