#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 2e3 + 10;
int G[MAXN][MAXN], n, m, ans, vis[MAXN][MAXN], mx, mn;
void add_cell(int x, int y, int dx, int dy) {
if (x <= 0 || y <= 0 || x > n || y > m) return;
if (vis[x][y]) return;
vis[x][y] = true;
if (!mn) mn = mx = G[x][y];
mn = min(mn, G[x][y]);
mx = max(mx, G[x][y]);
add_cell(x + dx, y, dx, dy);
add_cell(x, y + dy, dx, dy);
}
inline void solve(int dx, int dy) {
mx = mn = 0;
vector<pair<int, pll>> vec;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
vec.push_back({G[i][j], {i, j}});
vis[i][j] = false;
}
}
sort(all(vec));
ans = min(ans, vec.back().X - vec.front().X);
int l = 0, r = int(vec.size()) - 1;
while (l < r) {
auto [xl, yl] = vec[l].Y;
auto [xr, yr] = vec[r].Y;
if (vis[xl][yl]) {
l++;
continue;
}
if (vis[xr][yr]) {
r--;
continue;
}
add_cell(xl, yl, dx, dy);
while (l <= r) {
auto [xl, yl] = vec[l].Y;
auto [xr, yr] = vec[r].Y;
if (vis[xl][yl]) {
l++;
continue;
}
if (vis[xr][yr]) {
r--;
continue;
}
break;
}
if (l <= r)
ans = min(ans, max(mx - mn, vec[r].X - vec[l].X));
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> G[i][j];
}
}
ans = numeric_limits<int>::max();
solve(1, 1);
solve(1, -1);
solve(-1, 1);
solve(-1, -1);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
25 ms |
3504 KB |
Output is correct |
17 |
Correct |
20 ms |
3764 KB |
Output is correct |
18 |
Correct |
21 ms |
3792 KB |
Output is correct |
19 |
Correct |
20 ms |
3780 KB |
Output is correct |
20 |
Correct |
18 ms |
3500 KB |
Output is correct |
21 |
Correct |
28 ms |
3892 KB |
Output is correct |
22 |
Correct |
21 ms |
3844 KB |
Output is correct |
23 |
Correct |
21 ms |
3908 KB |
Output is correct |
24 |
Correct |
18 ms |
3592 KB |
Output is correct |
25 |
Correct |
21 ms |
3904 KB |
Output is correct |
26 |
Correct |
22 ms |
3900 KB |
Output is correct |
27 |
Correct |
20 ms |
3864 KB |
Output is correct |
28 |
Correct |
21 ms |
3908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
25 ms |
3504 KB |
Output is correct |
17 |
Correct |
20 ms |
3764 KB |
Output is correct |
18 |
Correct |
21 ms |
3792 KB |
Output is correct |
19 |
Correct |
20 ms |
3780 KB |
Output is correct |
20 |
Correct |
18 ms |
3500 KB |
Output is correct |
21 |
Correct |
28 ms |
3892 KB |
Output is correct |
22 |
Correct |
21 ms |
3844 KB |
Output is correct |
23 |
Correct |
21 ms |
3908 KB |
Output is correct |
24 |
Correct |
18 ms |
3592 KB |
Output is correct |
25 |
Correct |
21 ms |
3904 KB |
Output is correct |
26 |
Correct |
22 ms |
3900 KB |
Output is correct |
27 |
Correct |
20 ms |
3864 KB |
Output is correct |
28 |
Correct |
21 ms |
3908 KB |
Output is correct |
29 |
Correct |
2653 ms |
108400 KB |
Output is correct |
30 |
Correct |
2570 ms |
110420 KB |
Output is correct |
31 |
Correct |
2711 ms |
110512 KB |
Output is correct |
32 |
Correct |
2736 ms |
110548 KB |
Output is correct |
33 |
Correct |
2351 ms |
121924 KB |
Output is correct |
34 |
Correct |
2749 ms |
110560 KB |
Output is correct |
35 |
Correct |
2497 ms |
144468 KB |
Output is correct |
36 |
Correct |
2158 ms |
139292 KB |
Output is correct |
37 |
Correct |
2550 ms |
127976 KB |
Output is correct |