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 rep(i,x) for(int i = 0; i < x; ++i)
using namespace std;
const int inf = 2e9;
signed main(){
ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
int n , m;
cin >> n >> m;
vector <vector<int>> a(n,vector<int>(m));
rep(i , n)
rep(j , m)
cin >> a[i][j];
int answer = 0;
vector<vector<int>>best;
/// 1 zone
best = vector<vector<int>>(n,vector<int>(m,inf));
for(int i = 0; i < n; ++i){
for(int j = m - 1; j >= 0; --j){
if(i>0)best[i][j]=min(best[i][j],best[i-1][j]);
if(j<m-1)best[i][j]=min(best[i][j],best[i][j+1]);
best[i][j]=min(best[i][j],a[i][j]-i+j);
}
}
rep(i,n)
rep(j , m)
answer = max(answer , a[i][j]-i+j-best[i][j]);
/// 2 zone
best = vector<vector<int>>(n,vector<int>(m,inf));
for(int i = n-1; i >=0; --i){
for(int j = m - 1; j >= 0; --j){
if(i<n-1)best[i][j]=min(best[i][j],best[i+1][j]);
if(j<m-1)best[i][j]=min(best[i][j],best[i][j+1]);
best[i][j]=min(best[i][j],a[i][j]+i+j);
}
}
rep(i,n)
rep(j , m)
answer = max(answer , a[i][j]+i+j-best[i][j]);
/// 3 zone
best = vector<vector<int>>(n,vector<int>(m,inf));
for(int i = n-1; i >=0; --i){
for(int j = 0; j < m; ++j){
if(i<n-1)best[i][j]=min(best[i][j],best[i+1][j]);
if(j>0)best[i][j]=min(best[i][j],best[i][j-1]);
best[i][j]=min(best[i][j],a[i][j]+i-j);
}
}
rep(i,n)
rep(j , m)
answer = max(answer , a[i][j]+i-j-best[i][j]);
/// 4 zone
best = vector<vector<int>>(n,vector<int>(m,inf));
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(i>0)best[i][j]=min(best[i][j],best[i-1][j]);
if(j>0)best[i][j]=min(best[i][j],best[i][j-1]);
best[i][j]=min(best[i][j],a[i][j]-i-j);
}
}
rep(i,n)
rep(j , m)
answer = max(answer , a[i][j]-i-j-best[i][j]);
cout << answer - 1;
return 0;
}
# | 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... |