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>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const int N = 1e3+5;
const ll INF = 1e9+5;
const int dirx[]={1,0,-1,0};
const int diry[]={0,-1,0,1};
struct field{
int x,y;
ll val,len;
field(int _x=0,int _y=0,ll _val=0,ll _len=0){
x=_x;
y=_y;
val=_val;
len=_len;
}
bool operator <(const field &rhs) const{
if(val!=rhs.val)return val<rhs.val;
if(len!=rhs.len)return len<rhs.len;
if(x!=rhs.x)return x<rhs.x;
return y<rhs.y;
}
};
int n,m;
ll a[N][N],mx,r[N][N];
map<field,bool>vis;
queue<field>q;
bool ok(int x,int y){
return x>=1&&x<=n&&y>=1&&y<=m;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
if(a[i][j]>mx)mx=a[i][j];
}
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)r[i][j]=INF;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==mx){
q.push(field(i,j,mx,1));
while(!q.empty()){
auto e=q.front();
q.pop();
for(int k=0;k<4;k++){
int newx=e.x+dirx[k];
int newy=e.y+diry[k];
if(!ok(newx,newy))continue;
ll newval=min(e.val,a[newx][newy]);
if(newval+e.len+1<r[newx][newy]){
r[newx][newy]=newval+e.len+1;
q.push(field(newx,newy,newval,e.len+1));
}
}
}
}
}
}
/*for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==mx){
q.push(field(i,j,mx,1));
r[i][j]=mx+1;
}else{
r[i][j]=INF;
}
}
}*/
/*while(!q.empty()){
auto e=q.front();
q.pop();
if(vis[e])continue;
ll len=e.len;
ll val=e.val;
for(int i=0;i<4;i++){
int newx=e.x+dirx[i];
int newy=e.y+diry[i];
if(!ok(newx,newy))continue;//not in the matrix
r[newx][newy]=min(r[newx][newy],min(e.val,a[newx][newy])+len+1);
q.push(field(newx,newy,min(a[newx][newy],e.val),len+1));
}
}*/
ll ans=-2LL*INF;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
ans=max(ans,mx-r[i][j]);
}
}
cout<<ans;
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... |