#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 3e6 + 100;
int n, m, mn = 1e9, mx = 0;
vector < vector <int>> a, b, c, d;
bool check(int lim, vector < vector <int>> &a) {
vector < vector <int>> val(n + 1, vector <int> (m + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] - mn <= lim) {
val[i][j]++;
}
if (mx - a[i][j] <= lim) {
val[i][j] += 2;
}
if (val[i][j] == 0)
return 0;
}
}
int lst = 0, cur = m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= cur; j++) {
if (val[i][j] == 3 || val[i][j] == lst)
continue;
if (lst == 0) {
lst = val[i][j];
} else {
cur = j - 1;
break;
}
}
for (int j = cur + 1; j <= m; j++) {
if (val[i][j] != 3 && val[i][j] == lst)
return 0;
}
}
return 1;
}
void solve() {
cin >> n >> m;
a.assign(n + 1, vector <int> (m + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
b = a, c = a;
for (int i = 1; i <= n / 2; i++) {
swap(b[i], b[n - i + 1]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m / 2; j++) {
swap(c[i][j], c[i][m - j + 1]);
}
}
d = c;
for (int i = 1; i <= n / 2; i++) {
swap(d[i], d[n - i + 1]);
}
int l = 0, r = mx, m, ans = mx - mn;
while (l <= r) {
m = (l + r) / 2;
if (check(m, a) || check(m, b)) {
ans = m;
r = m - 1;
} else {
l = m + 1;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
//cin >> t;
while (t--) {
solve();
//cout << solve() << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |