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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 2e9;
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
int mn = inf; int mx = 0;
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];
smin(mn, a[i][j]);
smax(mx, a[i][j]);
}
}
auto Can = [&] (int x) {
vector<int> v(n);
int gde = m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < gde; j++) {
if (mn + x < a[i][j]) {
gde = j;
break;
}
}
v[i] = gde;
}
for (int i = 0; i < n; i++) {
for (int j = v[i]; j < m; j++) {
if (a[i][j] + x < mx) return 0;
}
}
return 1;
};
auto Solve = [&] () {
int l = 0, r = mx - mn, ans = r;
while (l <= r) {
int mid = l + r >> 1;
if (Can(mid)) {
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
return ans;
};
int ans = Solve();
reverse(a.begin(), a.end());
smin(ans, Solve());
for (int i = 0; i < n; i++) reverse(a[i].begin(), a[i].end());
smin(ans, Solve());
reverse(a.begin(), a.end());
smin(ans, Solve());
cout << ans << en;
return 0;
}
Compilation message (stderr)
joioi.cpp: In lambda function:
joioi.cpp:49:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |