#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 2e18;
const int MAXN = 2e3 + 5;
int n, m, mx, mn;
int a[MAXN][MAXN];
int g[MAXN][MAXN];
void rotate() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
g[j][n - i + 1] = a[i][j];
}
}
swap(n, m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = g[i][j];
}
}
}
bool check(int x) {
int lim = m + 1;
for (int i = 1; i <= n; i++) {
int r = 0;
for (int j = 1; j <= min(lim, m); j++) {
if (mx - x <= a[i][j]) {
r = max(r, j);
} else {
break;
}
}
lim = r;
for (int j = r + 1; j <= m; j++) {
if (a[i][j] - x > mn) {
return false;
}
}
}
return true;
}
int search() {
int l = 0, r = 2e9 + 5;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return r;
}
signed main() {
cin >> n >> m;
mx = -INF, mn = INF;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
}
int ans = INF;
ans = min(ans, search());
rotate();
ans = min(ans, search());
rotate();
ans = min(ans, search());
rotate();
ans = min(ans, search());
ans = min(mx - mn, ans);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2488 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2488 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
9 ms |
8540 KB |
Output is correct |
17 |
Correct |
13 ms |
8900 KB |
Output is correct |
18 |
Correct |
13 ms |
8796 KB |
Output is correct |
19 |
Correct |
13 ms |
8796 KB |
Output is correct |
20 |
Correct |
12 ms |
8932 KB |
Output is correct |
21 |
Correct |
15 ms |
9052 KB |
Output is correct |
22 |
Correct |
14 ms |
9052 KB |
Output is correct |
23 |
Correct |
14 ms |
9052 KB |
Output is correct |
24 |
Correct |
13 ms |
9052 KB |
Output is correct |
25 |
Correct |
14 ms |
9048 KB |
Output is correct |
26 |
Correct |
15 ms |
9048 KB |
Output is correct |
27 |
Correct |
14 ms |
9052 KB |
Output is correct |
28 |
Correct |
14 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2488 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
9 ms |
8540 KB |
Output is correct |
17 |
Correct |
13 ms |
8900 KB |
Output is correct |
18 |
Correct |
13 ms |
8796 KB |
Output is correct |
19 |
Correct |
13 ms |
8796 KB |
Output is correct |
20 |
Correct |
12 ms |
8932 KB |
Output is correct |
21 |
Correct |
15 ms |
9052 KB |
Output is correct |
22 |
Correct |
14 ms |
9052 KB |
Output is correct |
23 |
Correct |
14 ms |
9052 KB |
Output is correct |
24 |
Correct |
13 ms |
9052 KB |
Output is correct |
25 |
Correct |
14 ms |
9048 KB |
Output is correct |
26 |
Correct |
15 ms |
9048 KB |
Output is correct |
27 |
Correct |
14 ms |
9052 KB |
Output is correct |
28 |
Correct |
14 ms |
9052 KB |
Output is correct |
29 |
Correct |
1174 ms |
85160 KB |
Output is correct |
30 |
Correct |
1105 ms |
84748 KB |
Output is correct |
31 |
Correct |
1183 ms |
86352 KB |
Output is correct |
32 |
Correct |
1173 ms |
86496 KB |
Output is correct |
33 |
Correct |
1027 ms |
83028 KB |
Output is correct |
34 |
Correct |
1159 ms |
86296 KB |
Output is correct |
35 |
Correct |
1394 ms |
101944 KB |
Output is correct |
36 |
Correct |
1198 ms |
96716 KB |
Output is correct |
37 |
Correct |
1348 ms |
102180 KB |
Output is correct |