#include <bits/stdc++.h>
// #define int long long
using namespace std;
int n, m, mx, mn;
vector<vector<int>> v;
int f1(int mid)
{
int x = 0;
for (int i = 0; i < n; ++i)
{
int cur = m - 1;
for (; cur >= 0; --cur)
if (v[i][cur] < mx - mid)
break;
++cur;
x = max(x, cur);
for (int j = 0; j < x; ++j)
if (v[i][j] > mn + mid)
return 0;
}
return 1;
}
int f2(int mid)
{
int x = 0;
for (int i = 0; i < n; ++i)
{
int cur = m - 1;
for (; cur >= 0; --cur)
if (v[i][cur] > mn + mid)
break;
++cur;
x = max(x, cur);
for (int j = 0; j < x; ++j)
if (v[i][j] < mx - mid)
return 0;
}
return 1;
}
int f3(int mid)
{
int x = m - 1;
for (int i = 0; i < n; ++i)
{
int cur = 0;
for (; cur < m; ++cur)
if (v[i][cur] < mx - mid)
break;
--cur;
x = min(x, cur);
for (int j = x + 1; j < m; ++j)
if (v[i][j] > mn + mid)
return 0;
}
return 1;
}
int f4(int mid)
{
int x = m - 1;
for (int i = 0; i < n; ++i)
{
int cur = 0;
for (; cur < m; ++cur)
if (v[i][cur] > mn + mid)
break;
--cur;
x = min(x, cur);
for (int j = x + 1; j < m; ++j)
if (v[i][j] < mx - mid)
return 0;
}
return 1;
}
void solve()
{
cin >> n >> m;
v.assign(n, vector<int>(m));
mx = 0, mn = INT_MAX;
for (auto &i : v)
for (auto &j : i)
cin >> j, mx = max(mx, j), mn = min(mn, j);
int l = 0, r = 1000000005;
while (l < r)
{
int mid = (l + r) >> 1;
if (f1(mid) || f2(mid) || f3(mid) || f4(mid))
r = mid;
else
l = mid + 1;
}
cout << l << '\n';
}
/*
4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10
8 6
23 23 10 11 16 21
15 26 19 28 19 20
25 26 28 16 15 11
11 8 19 11 15 24
14 19 15 14 24 11
10 8 11 7 6 14
23 5 19 23 17 17
18 11 21 14 20 16
*/
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
616 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
616 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
856 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
856 KB |
Output is correct |
23 |
Correct |
4 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
4 ms |
976 KB |
Output is correct |
26 |
Correct |
5 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
980 KB |
Output is correct |
28 |
Correct |
4 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
616 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
856 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
856 KB |
Output is correct |
23 |
Correct |
4 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
4 ms |
976 KB |
Output is correct |
26 |
Correct |
5 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
980 KB |
Output is correct |
28 |
Correct |
4 ms |
856 KB |
Output is correct |
29 |
Correct |
246 ms |
26656 KB |
Output is correct |
30 |
Correct |
240 ms |
25940 KB |
Output is correct |
31 |
Correct |
299 ms |
27736 KB |
Output is correct |
32 |
Correct |
322 ms |
28112 KB |
Output is correct |
33 |
Correct |
237 ms |
24404 KB |
Output is correct |
34 |
Correct |
300 ms |
28076 KB |
Output is correct |
35 |
Correct |
336 ms |
35872 KB |
Output is correct |
36 |
Correct |
295 ms |
31568 KB |
Output is correct |
37 |
Correct |
322 ms |
16432 KB |
Output is correct |