#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e3 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll BASE = 137;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m;
int mn;
int a[N][N];
bool dd[N][N], dd2[N][N];
bool prec[N][N], sufc[N][N];
bool check (int X) {
rep (i, 1, n)
rep (j, 1, m) dd[i][j] = 0;
static queue<pair<int,int>> Q;
rep (i, 1, n)
rep (j, 1, m) if (a[i][j] == mn) Q.push({i, j});
while (!Q.empty()) {
pair<int,int> u = Q.front();
Q.pop();
rep (k, 0, 3) {
int ni = u.fs + dx[k], nj = u.se + dy[k];
if (ni >= 1 && ni <= n && nj >= 1 && nj <= m && dd[ni][nj] == 0 && a[ni][nj] <= mn + X) {
dd[ni][nj] = 1;
Q.push({ni, nj});
}
}
}
{
rep (i, 1, n)
rep (j, 1, m) dd2[i][j] = 0;
int mnR = n;
rep (j, 1, m) {
rep (i, 1, mnR) {
if (dd[i][j] == 0) {
mnR = i - 1;
break;
}
else dd2[i][j] = 1;
}
}
// rep (i, 1, n)
// rep (j, 1, m) cout << dd2[i][j] <<" \n"[j == m];
int mn1 = INF, mn2 = INF, mx1 = 0, mx2 = 0;
rep (i, 1, n) rep (j, 1, m) if (dd2[i][j]) {
mn1 = min(mn1, a[i][j]);
mx1 = max(mx1, a[i][j]);
}
else {
mx2 = max(mx2, a[i][j]);
mn2 = min(mn2, a[i][j]);
}
// cout << max(mx2 - mn2, mx1 - mn1) <<"\n";
if (max(mx2 - mn2, mx1 - mn1) <= X) {
return 1;
}
}
{
rep (i, 1, n)
rep (j, 1, m) dd2[i][j] = 0;
int mnR = 1;
reb (j, m, 1) {
reb (i, n, mnR) {
if (dd[i][j] == 0) {
mnR = i + 1;
break;
}
else dd2[i][j] = 1;
}
}
int mn1 = INF, mn2 = INF, mx1 = 0, mx2 = 0;
rep (i, 1, n) rep (j, 1, m) if (dd2[i][j]) {
mn1 = min(mn1, a[i][j]);
mx1 = max(mx1, a[i][j]);
}
else {
mx2 = max(mx2, a[i][j]);
mn2 = min(mn2, a[i][j]);
}
// cout << mx2 <<" "<<mn2 <<"\n";
if (max(mx2 - mn2, mx1 - mn1) <= X) return 1;
}
{
rep (i, 1, n)
rep (j, 1, m) dd2[i][j] = 0;
int mnR = n;
reb (j, m, 1) {
rep (i, 1, mnR) {
if (dd[i][j] == 0) {
mnR = i - 1;
break;
}
else dd2[i][j] = 1;
}
}
int mn1 = INF, mn2 = INF, mx1 = 0, mx2 = 0;
rep (i, 1, n) rep (j, 1, m) if (dd2[i][j]) {
mn1 = min(mn1, a[i][j]);
mx1 = max(mx1, a[i][j]);
}
else {
mx2 = max(mx2, a[i][j]);
mn2 = min(mn2, a[i][j]);
}
// cout << mx2 <<" "<<mn2 <<"\n";
if (max(mx2 - mn2, mx1 - mn1) <= X) return 1;
}
{
rep (i, 1, n)
rep (j, 1, m) dd2[i][j] = 0;
int mnR = 1;
rep (j, 1, m) {
reb(i, n, mnR) {
if (dd[i][j] == 0) {
mnR = i + 1;
break;
}
else dd2[i][j] = 1;
}
}
int mn1 = INF, mn2 = INF, mx1 = 0, mx2 = 0;
rep (i, 1, n) rep (j, 1, m) if (dd2[i][j]) {
mn1 = min(mn1, a[i][j]);
mx1 = max(mx1, a[i][j]);
}
else {
mx2 = max(mx2, a[i][j]);
mn2 = min(mn2, a[i][j]);
}
// cout << mx2 <<" "<<mn2 <<"\n";
if (max(mx2 - mn2, mx1 - mn1) <= X) return 1;
}
return 0;
}
void solution () {
cin >> n >> m;
rep (i, 1, n)
rep (j, 1, m)
cin >> a[i][j];
mn = INF;
rep (i, 1, n)
rep (j, 1, m) {
if (a[i][j] < mn) mn = a[i][j];
}
// cout << check(18) <<"\n";
// return;
int lf = 0, rt = 1e9;
while (lf < rt) {
int mid = lf + rt >> 1;
if (check(mid)) rt = mid;
else lf = mid + 1;
}
cout << lf <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +2
4
1 2 3 4
2
0 3
2 1
*/
Compilation message
joioi.cpp: In function 'void solution()':
joioi.cpp:171:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
171 | int mid = lf + rt >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6608 KB |
Output is correct |
6 |
Correct |
0 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
0 ms |
6492 KB |
Output is correct |
12 |
Correct |
0 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6608 KB |
Output is correct |
6 |
Correct |
0 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
0 ms |
6492 KB |
Output is correct |
12 |
Correct |
0 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
23 ms |
8796 KB |
Output is correct |
17 |
Correct |
18 ms |
8796 KB |
Output is correct |
18 |
Correct |
13 ms |
8948 KB |
Output is correct |
19 |
Correct |
14 ms |
8772 KB |
Output is correct |
20 |
Correct |
18 ms |
8920 KB |
Output is correct |
21 |
Correct |
20 ms |
9052 KB |
Output is correct |
22 |
Correct |
14 ms |
9052 KB |
Output is correct |
23 |
Correct |
13 ms |
9048 KB |
Output is correct |
24 |
Correct |
11 ms |
9020 KB |
Output is correct |
25 |
Correct |
16 ms |
9052 KB |
Output is correct |
26 |
Correct |
20 ms |
9064 KB |
Output is correct |
27 |
Correct |
19 ms |
9052 KB |
Output is correct |
28 |
Correct |
19 ms |
9064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6608 KB |
Output is correct |
6 |
Correct |
0 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
0 ms |
6492 KB |
Output is correct |
12 |
Correct |
0 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
23 ms |
8796 KB |
Output is correct |
17 |
Correct |
18 ms |
8796 KB |
Output is correct |
18 |
Correct |
13 ms |
8948 KB |
Output is correct |
19 |
Correct |
14 ms |
8772 KB |
Output is correct |
20 |
Correct |
18 ms |
8920 KB |
Output is correct |
21 |
Correct |
20 ms |
9052 KB |
Output is correct |
22 |
Correct |
14 ms |
9052 KB |
Output is correct |
23 |
Correct |
13 ms |
9048 KB |
Output is correct |
24 |
Correct |
11 ms |
9020 KB |
Output is correct |
25 |
Correct |
16 ms |
9052 KB |
Output is correct |
26 |
Correct |
20 ms |
9064 KB |
Output is correct |
27 |
Correct |
19 ms |
9052 KB |
Output is correct |
28 |
Correct |
19 ms |
9064 KB |
Output is correct |
29 |
Correct |
1894 ms |
47256 KB |
Output is correct |
30 |
Correct |
2000 ms |
46932 KB |
Output is correct |
31 |
Correct |
1644 ms |
48820 KB |
Output is correct |
32 |
Correct |
1871 ms |
48724 KB |
Output is correct |
33 |
Correct |
1313 ms |
45136 KB |
Output is correct |
34 |
Correct |
2116 ms |
48720 KB |
Output is correct |
35 |
Correct |
1900 ms |
64204 KB |
Output is correct |
36 |
Correct |
1090 ms |
59228 KB |
Output is correct |
37 |
Correct |
2397 ms |
64308 KB |
Output is correct |