#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
constexpr bool typetest = 0;
constexpr int N = 2e3 + 5;
const int Inf = 1e9 + 7;
int m, n, maxn(-Inf);
int a[N][N], b[N][N], smin[N][N], smax[N][N];
int pmin[N][N], pmax[N][N], s[N], p[N];
void Read()
{
cin >> m >> n;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
{
cin >> a[i][j];
maxn = max(maxn, a[i][j]);
}
}
bool Check(int v)
{
for (int i = 1; i <= m; ++i)
{
s[i] = 0;
p[i] = n + 1;
for (int j = 1; j <= n; ++j)
if (a[i][j] < maxn - v)
{
p[i] = min(p[i], j);
s[i] = max(s[i], j);
}
}
int maxx(-Inf), minx(Inf), piv(n + 1);
for (int i = m; i; --i)
{
piv = min(piv, p[i]);
maxx = max(maxx, smax[i][piv]);
minx = min(minx, smin[i][piv]);
}
if (maxx - minx <= v)
return true;
maxx = -Inf;
minx = Inf;
piv = 0;
for (int i = 1; i <= m; ++i)
{
piv = max(piv, s[i]);
maxx = max(maxx, pmax[i][piv]);
minx = min(minx, pmin[i][piv]);
}
return (maxx - minx <= v);
}
int Cal()
{
for (int i = 1; i <= m; ++i)
{
smin[i][n + 1] = pmin[i][0] = Inf;
smax[i][n + 1] = pmax[i][0] = -Inf;
for (int j = 1; j <= n; ++j)
{
pmin[i][j] = min(a[i][j], pmin[i][j - 1]);
pmax[i][j] = max(a[i][j], pmax[i][j - 1]);
}
for (int j = n; j; --j)
{
smin[i][j] = min(smin[i][j + 1], a[i][j]);
smax[i][j] = max(smax[i][j + 1], a[i][j]);
}
}
//cerr << "ok !\n";
int l = 0, mid, h = maxn;
while (l <= h)
{
mid = (l + h) / 2;
if (Check(mid))
h = mid - 1;
else
l = mid + 1;
}
return l;
}
void Solve()
{
int ans = Cal();
//cerr << "ok ? \n";
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
b[n - j + 1][i] = a[i][j];
swap(m, n);
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
a[i][j] = b[i][j];
cout << min(ans, Cal());
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
Read();
Solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
528 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
576 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
528 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
576 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
5196 KB |
Output is correct |
16 |
Correct |
7 ms |
6112 KB |
Output is correct |
17 |
Correct |
11 ms |
6264 KB |
Output is correct |
18 |
Correct |
11 ms |
6348 KB |
Output is correct |
19 |
Correct |
11 ms |
6348 KB |
Output is correct |
20 |
Correct |
12 ms |
6288 KB |
Output is correct |
21 |
Correct |
13 ms |
6476 KB |
Output is correct |
22 |
Correct |
14 ms |
6372 KB |
Output is correct |
23 |
Correct |
13 ms |
6476 KB |
Output is correct |
24 |
Correct |
12 ms |
6348 KB |
Output is correct |
25 |
Correct |
15 ms |
6432 KB |
Output is correct |
26 |
Correct |
12 ms |
6476 KB |
Output is correct |
27 |
Correct |
12 ms |
6496 KB |
Output is correct |
28 |
Correct |
12 ms |
6496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
528 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
576 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
5196 KB |
Output is correct |
16 |
Correct |
7 ms |
6112 KB |
Output is correct |
17 |
Correct |
11 ms |
6264 KB |
Output is correct |
18 |
Correct |
11 ms |
6348 KB |
Output is correct |
19 |
Correct |
11 ms |
6348 KB |
Output is correct |
20 |
Correct |
12 ms |
6288 KB |
Output is correct |
21 |
Correct |
13 ms |
6476 KB |
Output is correct |
22 |
Correct |
14 ms |
6372 KB |
Output is correct |
23 |
Correct |
13 ms |
6476 KB |
Output is correct |
24 |
Correct |
12 ms |
6348 KB |
Output is correct |
25 |
Correct |
15 ms |
6432 KB |
Output is correct |
26 |
Correct |
12 ms |
6476 KB |
Output is correct |
27 |
Correct |
12 ms |
6496 KB |
Output is correct |
28 |
Correct |
12 ms |
6496 KB |
Output is correct |
29 |
Correct |
706 ms |
116572 KB |
Output is correct |
30 |
Correct |
730 ms |
114992 KB |
Output is correct |
31 |
Correct |
825 ms |
117608 KB |
Output is correct |
32 |
Correct |
748 ms |
117640 KB |
Output is correct |
33 |
Correct |
688 ms |
113988 KB |
Output is correct |
34 |
Correct |
827 ms |
117752 KB |
Output is correct |
35 |
Correct |
963 ms |
115840 KB |
Output is correct |
36 |
Correct |
964 ms |
125748 KB |
Output is correct |
37 |
Correct |
1109 ms |
115764 KB |
Output is correct |