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 int long long
#define F first
#define S second
#define pb push_back
#define B begin()
#define E end()
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int a[1005][1005];
int dp[1005][1005];
int dp2[1005][1005];
int dp3[1005][1005];
int dp4[1005][1005];
signed main()
{
int n,m;
cin>>n>>m;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
cin>>a[i][j];
}
} // cout
memset(dp,-1,sizeof dp);
memset(dp3,-1,sizeof dp3);
memset(dp4,-1,sizeof dp4);
memset(dp2,-1,sizeof dp2);
// cout<<endl;
dp[0][0]=a[0][0]+1;
for(int i=1; i<n; i++)
{
dp[i][0]=min(dp[i-1][0]+1,a[i][0]+1);
}
for(int i=1; i<m; i++)
{
dp[0][i]=min(dp[0][i-1]+1,a[0][i]+1);
}
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
{
dp[i][j]=min( {a[i][j]+1,dp[i-1][j]+1,dp[i][j-1]+1} );
}
}
dp2[0][m-1]=a[0][m-1]+1;
for(int i=1; i<n; i++)
{
dp2[i][m-1]=min(dp2[i-1][m-1]+1,a[i][m-1]+1);
}
for(int i=m-2; i>=0; i--)
{
dp2[0][i]=min(dp2[0][i+1]+1,a[0][i]+1);
}
for(int i=1; i<n; i++)
{
for(int j=m-2; j>=0; j--)
{
dp2[i][j]=min( {a[i][j]+1,dp2[i-1][j]+1,dp2[i][j+1]+1} );
}
}
//int mx=0;
dp3[n-1][0]=a[n-1][0]+1;
for(int i=n-2; i>=0; i--)
{
dp3[i][0]=min(dp3[i+1][0]+1,a[i][0]+1);
}
for(int i=1; i<m; i++)
{
dp3[n-1][i]=min(dp3[n-1][i-1]+1,a[n-1][i]+1);
}
for(int i=n-2;i>=0;i--)
{
for(int j=1;j<m;j++)
{
dp3[i][j]=min( {a[i][j]+1, dp3[i+1][j]+1,dp3[i][j-1]+1} );
}
}
dp4[n-1][m-1]=a[n-1][m-1]+1;
for(int i=n-2; i>=0; i--)
{
dp4[i][m-1]=min(dp4[i+1][m-1]+1,a[i][m-1]+1);
}
for(int i=m-2; i>=0; i--)
{
dp4[n-1][i]=min(dp4[n-1][i+1]+1,a[n-1][i]+1);
}
for(int i=n-2;i>=0;i--)
{
for(int j=m-2;j>=0;j--)
{
dp4[i][j]=min( { a[i][j]+1,dp4[i+1][j]+1,dp4[i][j+1]+1 } );
}
}
int mx=-1;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
mx= max(mx,a[i][j]- min({dp[i][j],dp2[i][j],dp3[i][j],dp4[i][j]}));
}
}
cout<<mx<<endl;
}
//111
# | 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... |