#include<bits/stdc++.h>
using namespace std;
#define task "a"
#define se second
#define fi first
#define ll long long
#define ii pair<ll, ll>
#define tp tuple<int, int, int>
const long mxN = 2e3 + 7, inf = 1e9 + 7;
int nRow, nCol, a[mxN][mxN], ctr[mxN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(task".INP", "r", stdin);
//freopen(task".OUT", "w", stdout);
cin >> nRow >> nCol;
int mxx = 0, mnn = inf;
for (int i = 1; i <= nRow; i++)
{
for (int j = 1; j <= nCol; j++)
{
cin >> a[i][j];
mxx = max(mxx, a[i][j]);
mnn = min(a[i][j], mnn);
}
}
int ans = inf;
for (int k = 0; k < 4; k++)
{
vector<tp> point;
for (int i = 1; i <= nRow; i++)
{
ctr[i] = 0;
for (int j = 1; j <= nCol; j++)
point.push_back({a[i][j], i, j});
}
sort(point.begin(), point.end());
int mx = 0;
for (int id = 0; id < point.size(); id++)
{
auto [val, i, j] = point[id];
for (; i <= nRow; i++)
{
if (ctr[i] >= j)
break;
while (ctr[i] < j)
{
ctr[i]++;
mx = max(mx, a[i][ctr[i]]);
}
}
if (ctr[1] == nCol)
break;
ans = min(ans, max(mx - mnn, mxx - get<0>(point[id + 1])));
}
if (k % 2)
{
for (int i = 1; i <= nRow / 2; i++)
{
for (int j = 1; j <= nCol; j++)
swap(a[i][j], a[nRow - i + 1][j]);
}
}
else
{
for (int i = 1; i <= nRow; i++)
{
for (int j = 1; j <= nCol / 2; j++)
swap(a[i][j], a[i][nCol - j + 1]);
}
}
}
cout << 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... |