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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9 + 9;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
int minn = INF, px = 0, py = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> a[i][j];
if (a[i][j] < minn){
minn = a[i][j];
px = i, py = j;
}
}
}
auto check = [&](int x) -> bool {
auto _check = [&]() -> bool {
vector<vector<bool>> tag(n, vector<bool>(m));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (a[i][j] > minn + x){
tag[i][j] = 1;
}
}
}
vector<vector<bool>> vis(n, vector<bool>(m));
int maxn = m;
for (int i = 0; i < n; i++){
for (int j = 0; j < maxn; j++){
if (tag[i][j]){
maxn = j;
break;
}
vis[i][j] = 1;
}
}
if (!vis[px][py]) return false;
int mn = INF, mx = -INF;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (!vis[i][j]){
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
}
return mx - mn <= x;
};
bool ret = 0;
for (int t = 0; t < 2; t++){
ret |= _check();
reverse(a.begin(), a.end());
px = n - 1 - px;
ret |= _check();
for (int i = 0; i < n; i++){
reverse(a[i].begin(), a[i].end());
}
py = m - 1 - py;
}
return ret;
};
int l = -1, r = INF;
while (l < r - 1){
int mid = (l + r) / 2;
if (check(mid)){
r = mid;
}else{
l = mid;
}
}
cout << r;
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... |