#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 547
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll inf=1e18;
const ll mod=1e9+7;
const ld pai=acos(-1);
int n , m , I , J ;
pair < int , pi > MX ;
int a [2009][2009] , done [2009][2009] ;
int yes ( int x ) {
int mn = 1e9 , mx = -1e9 ;
for ( int i = 0 ; i < n ; i ++ ) {
for ( int j = 0 ; j < m ; j ++ ) {
if ( done [i][j] ) C ;
mn = min ( mn , a [i][j] ) ;
mx = max ( mx , a [i][j] ) ;
}
}
return ( mx - mn <= x ) ;
}
int check1 ( int x ) {
mem ( done , 0 ) ;
int last = 1e9 , mx = MX.fi , mn = 1e9 ;
for ( int i = 0 ; i < n ; i ++ ) {
int crnt = 0 ;
for ( int j = 0 ; j < m ; j ++ ) {
if ( crnt == last ) break ;
int MN = min ( mn , a [i][j] ) ;
if ( mx - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
crnt ++ ;
done [i][j] = 1 ;
}
if ( i == I ) {
if ( crnt < J+1 ) return 0 ;
}
}
return yes ( x ) ;
}
int check2 ( int x ) {
mem ( done , 0 ) ;
int last = 1e9 , mx = MX.fi , mn = 1e9 ;
for ( int i = n-1 ; i >=0 ; i -- ) {
int crnt = 0 ;
for ( int j = 0 ; j < m ; j ++ ) {
if ( crnt == last ) break ;
int MN = min ( mn , a [i][j] ) ;
if ( mx - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
crnt ++ ;
done [i][j] = 1 ;
}
if ( i == I ) {
if ( crnt < J+1 ) return 0 ;
}
}
return yes ( x ) ;
}
int check3 ( int x ) {
mem ( done , 0 ) ;
int last = 1e9 , mx = MX.fi , mn = 1e9 ;
for ( int i = 0 ; i < n ; i ++ ) {
int crnt = 0 ;
for ( int j = m -1 ; j >= 0 ; j -- ) {
if ( crnt == last ) break ;
int MN = min ( mn , a [i][j] ) ;
if ( mx - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
crnt ++ ;
done [i][j] = 1 ;
}
if ( i == I ) {
if ( crnt < m - J ) return 0 ;
}
}
return yes ( x ) ;
}
int check4 ( int x ) {
mem ( done , 0 ) ;
int last = 1e9 , mx = MX.fi , mn = 1e9 ;
for ( int i = n-1 ; i >=0 ; i -- ) {
int crnt = 0 ;
for ( int j = m -1 ; j >= 0 ; j -- ) {
if ( crnt == last ) break ;
int MN = min ( mn , a [i][j] ) ;
if ( mx - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
crnt ++ ;
done [i][j] = 1 ;
}
if ( i == I ) {
if ( crnt < m - J ) return 0 ;
}
}
return yes ( x ) ;
}
int main () {
cin >> n >> m ;
for ( int i = 0 ; i < n ; i ++ ) {
for ( int j = 0 ; j < m ; j ++ ) {
cin >> a [i][j] ;
MX = max ( MX , { a[i][j] , { i , j } } ) ;
}
}
I = MX.se.fi ;
J = MX.se.se ;
int l = -1 , r = 2e9 ;
while ( r - l > 1 ) {
if ( check1 ( mid ) || check2 ( mid ) || check3 ( mid ) || check4 ( mid ) ) r = mid ;
else l = mid ;
}
cout << r << endl ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
16128 KB |
Output is correct |
2 |
Correct |
83 ms |
16128 KB |
Output is correct |
3 |
Correct |
205 ms |
16204 KB |
Output is correct |
4 |
Correct |
221 ms |
16128 KB |
Output is correct |
5 |
Correct |
178 ms |
16128 KB |
Output is correct |
6 |
Correct |
208 ms |
16204 KB |
Output is correct |
7 |
Correct |
208 ms |
16128 KB |
Output is correct |
8 |
Correct |
223 ms |
16128 KB |
Output is correct |
9 |
Correct |
216 ms |
16248 KB |
Output is correct |
10 |
Correct |
230 ms |
16128 KB |
Output is correct |
11 |
Correct |
178 ms |
16128 KB |
Output is correct |
12 |
Correct |
196 ms |
16208 KB |
Output is correct |
13 |
Correct |
242 ms |
16248 KB |
Output is correct |
14 |
Correct |
165 ms |
16128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
16128 KB |
Output is correct |
2 |
Correct |
83 ms |
16128 KB |
Output is correct |
3 |
Correct |
205 ms |
16204 KB |
Output is correct |
4 |
Correct |
221 ms |
16128 KB |
Output is correct |
5 |
Correct |
178 ms |
16128 KB |
Output is correct |
6 |
Correct |
208 ms |
16204 KB |
Output is correct |
7 |
Correct |
208 ms |
16128 KB |
Output is correct |
8 |
Correct |
223 ms |
16128 KB |
Output is correct |
9 |
Correct |
216 ms |
16248 KB |
Output is correct |
10 |
Correct |
230 ms |
16128 KB |
Output is correct |
11 |
Correct |
178 ms |
16128 KB |
Output is correct |
12 |
Correct |
196 ms |
16208 KB |
Output is correct |
13 |
Correct |
242 ms |
16248 KB |
Output is correct |
14 |
Correct |
165 ms |
16128 KB |
Output is correct |
15 |
Correct |
85 ms |
16128 KB |
Output is correct |
16 |
Correct |
98 ms |
17272 KB |
Output is correct |
17 |
Correct |
160 ms |
17528 KB |
Output is correct |
18 |
Correct |
209 ms |
17408 KB |
Output is correct |
19 |
Correct |
150 ms |
17272 KB |
Output is correct |
20 |
Correct |
161 ms |
17340 KB |
Output is correct |
21 |
Correct |
236 ms |
17528 KB |
Output is correct |
22 |
Correct |
212 ms |
17528 KB |
Output is correct |
23 |
Correct |
204 ms |
17664 KB |
Output is correct |
24 |
Correct |
262 ms |
17400 KB |
Output is correct |
25 |
Correct |
203 ms |
17528 KB |
Output is correct |
26 |
Correct |
248 ms |
17656 KB |
Output is correct |
27 |
Correct |
259 ms |
17528 KB |
Output is correct |
28 |
Correct |
223 ms |
17528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
16128 KB |
Output is correct |
2 |
Correct |
83 ms |
16128 KB |
Output is correct |
3 |
Correct |
205 ms |
16204 KB |
Output is correct |
4 |
Correct |
221 ms |
16128 KB |
Output is correct |
5 |
Correct |
178 ms |
16128 KB |
Output is correct |
6 |
Correct |
208 ms |
16204 KB |
Output is correct |
7 |
Correct |
208 ms |
16128 KB |
Output is correct |
8 |
Correct |
223 ms |
16128 KB |
Output is correct |
9 |
Correct |
216 ms |
16248 KB |
Output is correct |
10 |
Correct |
230 ms |
16128 KB |
Output is correct |
11 |
Correct |
178 ms |
16128 KB |
Output is correct |
12 |
Correct |
196 ms |
16208 KB |
Output is correct |
13 |
Correct |
242 ms |
16248 KB |
Output is correct |
14 |
Correct |
165 ms |
16128 KB |
Output is correct |
15 |
Correct |
85 ms |
16128 KB |
Output is correct |
16 |
Correct |
98 ms |
17272 KB |
Output is correct |
17 |
Correct |
160 ms |
17528 KB |
Output is correct |
18 |
Correct |
209 ms |
17408 KB |
Output is correct |
19 |
Correct |
150 ms |
17272 KB |
Output is correct |
20 |
Correct |
161 ms |
17340 KB |
Output is correct |
21 |
Correct |
236 ms |
17528 KB |
Output is correct |
22 |
Correct |
212 ms |
17528 KB |
Output is correct |
23 |
Correct |
204 ms |
17664 KB |
Output is correct |
24 |
Correct |
262 ms |
17400 KB |
Output is correct |
25 |
Correct |
203 ms |
17528 KB |
Output is correct |
26 |
Correct |
248 ms |
17656 KB |
Output is correct |
27 |
Correct |
259 ms |
17528 KB |
Output is correct |
28 |
Correct |
223 ms |
17528 KB |
Output is correct |
29 |
Correct |
1991 ms |
53396 KB |
Output is correct |
30 |
Correct |
1839 ms |
53380 KB |
Output is correct |
31 |
Correct |
2024 ms |
55160 KB |
Output is correct |
32 |
Correct |
1998 ms |
55060 KB |
Output is correct |
33 |
Correct |
1766 ms |
50280 KB |
Output is correct |
34 |
Correct |
1990 ms |
55108 KB |
Output is correct |
35 |
Correct |
2868 ms |
70848 KB |
Output is correct |
36 |
Correct |
2538 ms |
65656 KB |
Output is correct |
37 |
Correct |
2796 ms |
70940 KB |
Output is correct |