#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e3+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
priority_queue<pair<ll,pll> > pq;
ll n,m,u[N][N],vis[N][N],dg[N][N],ans,x,y;
ll dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
bool BFS(ll sx,ll sy,ll ty,ll D,ll k){
REP(i,n+2)REP(j,m+2){
dg[i][j]=(i==sx?1:0)+(j==sy?1:0);
vis[i][j]=0;
}
ll mn=u[sx][sy],mx=u[sx][sy];
pq.push(mp(mx,mp(sx,sy)));
while(SZ(pq)){
auto tmp=pq.top();pq.pop();
if(max(mx,tmp.X)-min(mn,tmp.X)>k)continue;
x=tmp.Y.X;y=tmp.Y.Y;
vis[x][y]=1;mn=min(mn,tmp.X);mx=max(mx,tmp.X);
REP(i,4)if((1LL<<i)&D){
ll nx=x+dx[i],ny=y+dy[i];
++dg[nx][ny];
if(dg[nx][ny]==2){
pq.push(mp(u[nx][ny],mp(nx,ny)));
}
}
}
mn=INF;mx=-INF;
REP1(i,n)REP1(j,m){
if(!vis[i][j])mn=min(mn,u[i][j]),mx=max(mx,u[i][j]);
}
return (mx-mn>k?0:1);
}
bool ok(ll k){
REP(i,2)if(BFS(1,1,i,6,k)||BFS(1,m,i,12,k)||BFS(n,1,i,3,k)||BFS(n,m,i,9,k))return 1;
return 0;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
REP1(i,n)REP1(j,m)cin>>u[i][j];
for(int i=31;i>=0;i--){
if(!ok(ans+(1LL<<i)))ans+=(1LL<<i);
}
cout<<ans+1<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
85 ms |
3832 KB |
Output is correct |
17 |
Correct |
130 ms |
4096 KB |
Output is correct |
18 |
Correct |
76 ms |
4096 KB |
Output is correct |
19 |
Correct |
98 ms |
4096 KB |
Output is correct |
20 |
Correct |
76 ms |
3584 KB |
Output is correct |
21 |
Correct |
223 ms |
4344 KB |
Output is correct |
22 |
Correct |
106 ms |
4096 KB |
Output is correct |
23 |
Correct |
105 ms |
4224 KB |
Output is correct |
24 |
Correct |
90 ms |
3832 KB |
Output is correct |
25 |
Correct |
105 ms |
4224 KB |
Output is correct |
26 |
Correct |
134 ms |
4224 KB |
Output is correct |
27 |
Correct |
171 ms |
4224 KB |
Output is correct |
28 |
Correct |
183 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
85 ms |
3832 KB |
Output is correct |
17 |
Correct |
130 ms |
4096 KB |
Output is correct |
18 |
Correct |
76 ms |
4096 KB |
Output is correct |
19 |
Correct |
98 ms |
4096 KB |
Output is correct |
20 |
Correct |
76 ms |
3584 KB |
Output is correct |
21 |
Correct |
223 ms |
4344 KB |
Output is correct |
22 |
Correct |
106 ms |
4096 KB |
Output is correct |
23 |
Correct |
105 ms |
4224 KB |
Output is correct |
24 |
Correct |
90 ms |
3832 KB |
Output is correct |
25 |
Correct |
105 ms |
4224 KB |
Output is correct |
26 |
Correct |
134 ms |
4224 KB |
Output is correct |
27 |
Correct |
171 ms |
4224 KB |
Output is correct |
28 |
Correct |
183 ms |
4344 KB |
Output is correct |
29 |
Execution timed out |
4073 ms |
112248 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |