#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 2100
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
int arr[MAX][MAX];
pii range[MAX];
int N, M;
bool rchk() {
int i;
int mnn = -1;
bool cc = true;
for (i = 1; i <= N; i++) {
if (mnn < range[i].first) mnn = range[i].first;
else {
if (mnn > range[i].second) {
cc = false;
break;
}
}
}
mnn = -1;
if (!cc) {
cc = true;
for (i = N; i >= 1; i--) {
if (mnn < range[i].first) mnn = range[i].first;
else {
if (mnn > range[i].second) {
cc = false;
break;
}
}
}
}
return cc;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> N >> M;
int i, j;
int mn = 1000000010;
int mx = 0;
for (i = 1; i <= N; i++) {
for (j = 1; j <= M; j++) {
cin >> arr[i][j];
mx = max(arr[i][j], mx);
mn = min(arr[i][j], mn);
}
}
if (mx == mn) {
cout << 0 << ln;
return 0;
}
int l, r;
l = -1;
r = mx - mn;
int mid;
while (r - l > 1) {
mid = (l + r + 1) / 2;
bool chk = true;
for (i = 1; i <= N; i++) {
range[i] = { 1, M };
for (j = 1; j <= M; j++) if (mx - arr[i][j] > mid) range[i].first = j;
for (j = M; j >= 1; j--) if (arr[i][j] - mn > mid) range[i].second = j;
if (range[i].first > range[i].second) {
chk = false;
break;
}
}
if (chk) chk = rchk();
if (!chk) {
chk = true;
for (i = 1; i <= N; i++) {
range[i] = { 1, M };
for (j = 1; j <= M; j++) if (arr[i][j] - mn > mid) range[i].first = j;
for (j = M; j >= 1; j--) if (mx - arr[i][j] > mid) range[i].second = j;
if (range[i].first > range[i].second) {
chk = false;
break;
}
}
}
if (chk) r = mid;
else l = mid;
}
cout << r << ln;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |