This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
// #define int long long
using namespace std;
void solve()
{
int n, m;
cin >> n >> m;
vector<vector<int>> v(n, vector<int>(m));
for (auto &i : v)
for (auto &j : i)
cin >> j;
int l = 0, r = 1000000005;
while (l < r)
{
int mid = (l + r) >> 1, ok = 1;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i)
{
int mx = v[i][0], mn = mx;
for (a[i] = 0; a[i] < m; ++a[i])
{
mx = max(mx, v[i][a[i]]);
mn = min(mn, v[i][a[i]]);
if (mx - mn > mid)
break;
}
--a[i];
}
for (int i = 0; i < n; ++i)
{
int mx = v[i][m - 1], mn = mx;
for (b[i] = m - 1; b[i] >= 0; --b[i])
{
mx = max(mx, v[i][b[i]]);
mn = min(mn, v[i][b[i]]);
if (mx - mn > mid)
break;
}
++b[i];
if (b[i] > a[i] + 1)
{
ok = 0;
break;
}
}
if (ok == 0)
{
l = mid + 1;
continue;
}
int mn = a[0];
for (int i = 1; i < n; ++i)
{
mn = min(mn, a[i]);
if (b[i] > mn + 1)
{
ok = 0;
break;
}
}
if (ok)
{
r = mid;
continue;
}
int mx = b[0];
for (int i = 1; i < n; ++i)
{
mx = max(mx, b[i]);
if (mx > a[i] + 1)
{
ok = 0;
break;
}
}
if (ok)
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
*/
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |