#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9 + 9;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
int minn = INF, px = 0, py = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> a[i][j];
if (a[i][j] < minn){
minn = a[i][j];
px = i, py = j;
}
}
}
auto check = [&](int x) -> bool {
auto _check = [&]() -> bool {
vector<vector<bool>> tag(n, vector<bool>(m));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (a[i][j] > minn + x){
tag[i][j] = 1;
}
}
}
vector<vector<bool>> vis(n, vector<bool>(m));
int maxn = m;
for (int i = 0; i < n; i++){
for (int j = 0; j < maxn; j++){
if (tag[i][j]){
maxn = j;
break;
}
vis[i][j] = 1;
}
}
if (!vis[px][py]) return false;
int mn = INF, mx = -INF;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (!vis[i][j]){
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
}
return mx - mn <= x;
};
bool ret = 0;
for (int t = 0; t < 2; t++){
ret |= _check();
reverse(a.begin(), a.end());
px = n - 1 - px;
ret |= _check();
for (int i = 0; i < n; i++){
reverse(a[i].begin(), a[i].end());
}
py = m - 1 - py;
}
return ret;
};
int l = -1, r = INF;
while (l < r - 1){
int mid = (l + r) / 2;
if (check(mid)){
r = mid;
}else{
l = mid;
}
}
cout << r;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
29 ms |
468 KB |
Output is correct |
17 |
Correct |
22 ms |
720 KB |
Output is correct |
18 |
Correct |
21 ms |
748 KB |
Output is correct |
19 |
Correct |
26 ms |
716 KB |
Output is correct |
20 |
Correct |
20 ms |
724 KB |
Output is correct |
21 |
Correct |
21 ms |
884 KB |
Output is correct |
22 |
Correct |
19 ms |
860 KB |
Output is correct |
23 |
Correct |
24 ms |
852 KB |
Output is correct |
24 |
Correct |
17 ms |
724 KB |
Output is correct |
25 |
Correct |
25 ms |
852 KB |
Output is correct |
26 |
Correct |
17 ms |
848 KB |
Output is correct |
27 |
Correct |
20 ms |
840 KB |
Output is correct |
28 |
Correct |
20 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
29 ms |
468 KB |
Output is correct |
17 |
Correct |
22 ms |
720 KB |
Output is correct |
18 |
Correct |
21 ms |
748 KB |
Output is correct |
19 |
Correct |
26 ms |
716 KB |
Output is correct |
20 |
Correct |
20 ms |
724 KB |
Output is correct |
21 |
Correct |
21 ms |
884 KB |
Output is correct |
22 |
Correct |
19 ms |
860 KB |
Output is correct |
23 |
Correct |
24 ms |
852 KB |
Output is correct |
24 |
Correct |
17 ms |
724 KB |
Output is correct |
25 |
Correct |
25 ms |
852 KB |
Output is correct |
26 |
Correct |
17 ms |
848 KB |
Output is correct |
27 |
Correct |
20 ms |
840 KB |
Output is correct |
28 |
Correct |
20 ms |
848 KB |
Output is correct |
29 |
Correct |
1673 ms |
30980 KB |
Output is correct |
30 |
Correct |
1821 ms |
18228 KB |
Output is correct |
31 |
Correct |
2143 ms |
31880 KB |
Output is correct |
32 |
Correct |
1693 ms |
31948 KB |
Output is correct |
33 |
Correct |
1539 ms |
35144 KB |
Output is correct |
34 |
Correct |
1935 ms |
31772 KB |
Output is correct |
35 |
Correct |
1373 ms |
23276 KB |
Output is correct |
36 |
Correct |
1485 ms |
30748 KB |
Output is correct |
37 |
Correct |
1901 ms |
46576 KB |
Output is correct |