Submission #204172

#TimeUsernameProblemLanguageResultExecution timeMemory
204172mdn2002The Kingdom of JOIOI (JOI17_joioi)C++14
0 / 100
5 ms380 KiB
#include<bits/stdc++.h> using namespace std; const long long mod=998244353; int xx[]={1,-1,0,0},yy[]={0,0,1,-1}; long long n,m,a[2050][2050],on[2050],of[2050],ans=1e18; multiset<pair<long long,pair<int,int> > >ms; multiset<long long>s,s1; bool vis[2050][2050]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen(".in","r",stdin); //freopen(".out","w",stdout); cin>>n>>m; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>a[i][j]; if(i==0||j==0||i==n-1||j==m-1)ms.insert({a[i][j],{i,j}}); s.insert(a[i][j]); } } pair<long long,pair<int,int> >z=*--ms.end(); s.erase(s.lower_bound(z.first)); ans=min(ans,*--s.end()-*s.begin()); s1.insert(z.first); ms.clear(); int x=z.second.first,y=z.second.second; vis[x][y]=1; on[x]=1,of[y]=1; for(int i=0;i<4;i++) { int tx=x+xx[i],ty=y+yy[i]; if(0<=tx&&tx<n&&0<=ty&&ty<m)ms.insert({a[tx][ty],{tx,ty}}); } while(s.size()>1) { pair<long long,pair<int,int> >z=*--ms.end(); ms.erase(--ms.end()); long long val=z.first,x=z.second.first,y=z.second.second; if(vis[x][y])continue; if(on[x]&&vis[x][y-1]==0&&vis[x][y+1]==0)continue; if(of[y]&&vis[x-1][y]==0&&vis[x+1][y]==0)continue; vis[x][y]=1; s.erase(s.lower_bound(val)); s1.insert(val); long long dif=max(*--s1.end()-*s1.begin(),*--s.end()-*s.begin()); ans=min(ans,dif); on[x]=1,of[y]=1; for(int i=0;i<4;i++) { int tx=x+xx[i],ty=y+yy[i]; if(vis[tx][ty])continue; if(0<=tx&&tx<n&&0<=ty&&ty<m) { if(i<2) { if(on[tx]==0)ms.insert({a[tx][ty],{tx,ty}}); else if(vis[tx][ty-1]||vis[tx][ty+1])ms.insert({a[tx][ty],{tx,ty}}); } if(2<=i) { if(of[ty]==0)ms.insert({a[tx][ty],{tx,ty}}); else if(vis[tx-1][ty]||vis[tx+1][ty])ms.insert({a[tx][ty],{tx,ty}}); } } } } cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...