#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 = (mx - mn + 1) / 2, 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
joioi.cpp: In lambda function:
joioi.cpp:49:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |