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 int long long
int mn = 1e11, mx = -1e11;
bool good(int n, int m, vector<vector<int>> &a, int x) {
int pos = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] <= mx - x) pos = max(j, pos);
}
for (int j = 0; j < m; j++) {
if (a[i][j] >= mn + x && j <= pos) return 0;
}
}
return 1;
}
int solve(int n, int m, vector<vector<int>> &a) {
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}*/
int l = -1, r = 2e9;
while (r - l > 1) {
int md = (l + r) / 2;
if (good(n, m, a, md)) {
//cout << "Good " << md << "\n";
r = md;
} else {
//cout << "Bad " << md << "\n";
l = md;
}
}
//cout << l << "\n\n";
//cout << "." << endl;
return l;
}
vector<vector<int>> flipv(int n, int m, vector<vector<int>> &a) {
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i][j] = a[i][m - j - 1];
}
}
return ans;
}
vector<vector<int>> flips(int n, int m, vector<vector<int>> &a) {
mn = -mn, mx = -mx;
swap(mn, mx);
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i][j] = -a[i][j];
}
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int ans = solve(n, m, a);
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
a = flipv(n, m, a);
ans = min(ans, solve(n, m, a));
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
a = flips(n, m, a);
ans = min(ans, solve(n, m, a));
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
a = flipv(n, m, a);
ans = min(ans, solve(n, m, a));
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |