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
bool good(int n, int m, vector<vector<int>> &a, int x) {
int mn = 1e11, mx = -1e11;
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]);
}
}
vector<vector<int>> need(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] <= mx - x) need[i][j] += 1;
if (a[i][j] >= mn + x) need[i][j] += 2;
if (need[i][j] == 3) return 0;
}
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
switch (need[i][j]) {
case 0: cout << " "; break;
case 1: cout << "-"; break;
case 2: cout << "+"; break;
case 3: cout << "±";
}
}
cout << "\n";
}*/
for (int i = 0; i < n; i++) {
int ch = 0, cur = -1;
for (int j = 0; j < m; j++) {
if (need[i][j] != 0 && need[i][j] != cur) {
ch++;
cur = need[i][j];
}
}
if (ch > 2) {
return 0;
}
}
for (int j = 0; j < m; j++) {
int ch = 0, cur = -1;
for (int i = 0; i < n; i++) {
if (need[i][j] != 0 && need[i][j] != cur) {
ch++;
cur = need[i][j];
}
}
if (ch > 2) {
return 0;
}
}
return 1;
}
int solve(int n, int m, vector<vector<int>> &a) {
int l = -1, r = 1e11;
while (r - l > 1) {
int md = (l + r) / 2;
if (good(n, m, a, md)) {
r = md;
} else {
l = md;
}
}
return l;
}
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)), a(n, vector<int>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a_[i][j];
int 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... |