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>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 2003;
const int INF = 1000000009;
int n, m;
int a[N][N];
bool stg(int x, int y, int z)
{
int maxu = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
maxu = max(maxu, a[i][j]);
}
if (maxu - a[x][y] <= z)
return true;
int min1 = a[x][y];
int max1 = 0;
int min2 = INF;
int max2 = 0;
int l = y;
int r = m;
for (int i = 1; i <= n; ++i)
{
if (l > r)
return false;
for (int j = 1; j <= l; ++j)
{
max1 = max(max1, a[i][j]);
}
for (int j = l + 1; j <= r; ++j)
{
if (a[i][j] - min1 > z)
{
r = j - 1;
break;
}
max1 = max(max1, a[i][j]);
}
for (int j = r + 1; j <= m; ++j)
{
min2 = min(min2, a[i][j]);
max2 = max(max2, a[i][j]);
}
if (i == x)
{
l = 0;
}
}
return (max1 - min1 <= z && max2 - min2 <= z);
}
int solvv()
{
int mini = 1;
int minj = 1;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (a[i][j] < a[mini][minj])
{
mini = i;
minj = j;
}
}
}
if (mini == n && minj == m)
return INF;
int l = 0, r = INF;
int ans;
while (l <= r)
{
int m = (l + r) / 2;
if (stg(mini, minj, m))
{
ans = m;
r = m - 1;
}
else
l = m + 1;
}
return ans;
}
void frt()
{
for (int i = 1; i <= n; ++i)
{
reverse(a[i] + 1, a[i] + m + 1);
}
}
void frs()
{
for (int j = 1; j <= m; ++j)
{
vector<int> v;
for (int i = 1; i <= n; ++i)
v.push_back(a[i][j]);
reverse(all(v));
for (int i = 1; i <= n; ++i)
a[i][j] = v[i - 1];
}
}
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
cin >> a[i][j];
}
int ans = solvv();
frt();
ans = min(ans, solvv());
frs();
ans = min(ans, solvv());
frt();
ans = min(ans, solvv());
cout << ans << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
Compilation message (stderr)
joioi.cpp: In function 'int solvv()':
joioi.cpp:85:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
85 | int ans;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |