#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 ;
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 = -1e9 , 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] ) ;
int MX = max ( mx , a [i][j] ) ;
if ( MX - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
mx = MX ;
crnt ++ ;
done [i][j] = 1 ;
}
}
return yes ( x ) ;
}
int check2 ( int x ) {
mem ( done , 0 ) ;
int last = 1e9 , mx = -1e9 , 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] ) ;
int MX = max ( mx , a [i][j] ) ;
if ( MX - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
mx = MX ;
crnt ++ ;
done [i][j] = 1 ;
}
}
return yes ( x ) ;
}
int check3 ( int x ) {
mem ( done , 0 ) ;
int last = 1e9 , mx = -1e9 , 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] ) ;
int MX = max ( mx , a [i][j] ) ;
if ( MX - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
mx = MX ;
crnt ++ ;
done [i][j] = 1 ;
}
}
return yes ( x ) ;
}
int check4 ( int x ) {
mem ( done , 0 ) ;
int last = 1e9 , mx = -1e9 , 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] ) ;
int MX = max ( mx , a [i][j] ) ;
if ( MX - MN > x ) {
last = crnt ;
break ;
}
mn = MN ;
mx = MX ;
crnt ++ ;
done [i][j] = 1 ;
}
}
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] ;
}
}
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 |
82 ms |
16128 KB |
Output is correct |
3 |
Correct |
178 ms |
16128 KB |
Output is correct |
4 |
Correct |
194 ms |
16128 KB |
Output is correct |
5 |
Correct |
172 ms |
16128 KB |
Output is correct |
6 |
Correct |
177 ms |
16248 KB |
Output is correct |
7 |
Correct |
188 ms |
16128 KB |
Output is correct |
8 |
Correct |
174 ms |
16128 KB |
Output is correct |
9 |
Correct |
210 ms |
16248 KB |
Output is correct |
10 |
Correct |
202 ms |
16128 KB |
Output is correct |
11 |
Correct |
231 ms |
16128 KB |
Output is correct |
12 |
Incorrect |
192 ms |
16248 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
16128 KB |
Output is correct |
2 |
Correct |
82 ms |
16128 KB |
Output is correct |
3 |
Correct |
178 ms |
16128 KB |
Output is correct |
4 |
Correct |
194 ms |
16128 KB |
Output is correct |
5 |
Correct |
172 ms |
16128 KB |
Output is correct |
6 |
Correct |
177 ms |
16248 KB |
Output is correct |
7 |
Correct |
188 ms |
16128 KB |
Output is correct |
8 |
Correct |
174 ms |
16128 KB |
Output is correct |
9 |
Correct |
210 ms |
16248 KB |
Output is correct |
10 |
Correct |
202 ms |
16128 KB |
Output is correct |
11 |
Correct |
231 ms |
16128 KB |
Output is correct |
12 |
Incorrect |
192 ms |
16248 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
16128 KB |
Output is correct |
2 |
Correct |
82 ms |
16128 KB |
Output is correct |
3 |
Correct |
178 ms |
16128 KB |
Output is correct |
4 |
Correct |
194 ms |
16128 KB |
Output is correct |
5 |
Correct |
172 ms |
16128 KB |
Output is correct |
6 |
Correct |
177 ms |
16248 KB |
Output is correct |
7 |
Correct |
188 ms |
16128 KB |
Output is correct |
8 |
Correct |
174 ms |
16128 KB |
Output is correct |
9 |
Correct |
210 ms |
16248 KB |
Output is correct |
10 |
Correct |
202 ms |
16128 KB |
Output is correct |
11 |
Correct |
231 ms |
16128 KB |
Output is correct |
12 |
Incorrect |
192 ms |
16248 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |