#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
int mn = 1e9, mx = 0;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int l = 0, r = 1e9, ans = -1;
auto check = [&](int mid) {
int lst = -1, ok = 1;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
if (a[i][j] > mn + mid)
lst = max(lst, j);
}
for (int j = 0;j < m;j++) {
if (j <= lst && a[i][j] < mx - mid)
ok = 0;
}
}
if (ok)
return ok;
reverse(a.begin(), a.end());
lst = -1;
ok = 1;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
if (a[i][j] > mn + mid)
lst = max(lst, j);
}
for (int j = 0;j < m;j++) {
if (j <= lst && a[i][j] < mx - mid)
ok = 0;
}
}
if (ok)
return ok;
for (int i = 0;i < n;i++)
reverse(a[i].begin(), a[i].end());
lst = -1;
ok = 1;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
if (a[i][j] > mn + mid)
lst = max(lst, j);
}
for (int j = 0;j < m;j++) {
if (j <= lst && a[i][j] < mx - mid)
ok = 0;
}
}
if (ok)
return ok;
reverse(a.begin(), a.end());
lst = -1;
ok = 1;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
if (a[i][j] > mn + mid)
lst = max(lst, j);
}
for (int j = 0;j < m;j++) {
if (j <= lst && a[i][j] < mx - mid)
ok = 0;
}
}
return ok;
};
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 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 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 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 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
600 KB |
Output is correct |
17 |
Correct |
6 ms |
856 KB |
Output is correct |
18 |
Correct |
8 ms |
856 KB |
Output is correct |
19 |
Correct |
10 ms |
860 KB |
Output is correct |
20 |
Correct |
10 ms |
860 KB |
Output is correct |
21 |
Correct |
11 ms |
1164 KB |
Output is correct |
22 |
Correct |
11 ms |
984 KB |
Output is correct |
23 |
Correct |
12 ms |
1116 KB |
Output is correct |
24 |
Correct |
10 ms |
1084 KB |
Output is correct |
25 |
Correct |
15 ms |
1112 KB |
Output is correct |
26 |
Correct |
10 ms |
1116 KB |
Output is correct |
27 |
Correct |
11 ms |
1016 KB |
Output is correct |
28 |
Correct |
10 ms |
1172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 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 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
600 KB |
Output is correct |
17 |
Correct |
6 ms |
856 KB |
Output is correct |
18 |
Correct |
8 ms |
856 KB |
Output is correct |
19 |
Correct |
10 ms |
860 KB |
Output is correct |
20 |
Correct |
10 ms |
860 KB |
Output is correct |
21 |
Correct |
11 ms |
1164 KB |
Output is correct |
22 |
Correct |
11 ms |
984 KB |
Output is correct |
23 |
Correct |
12 ms |
1116 KB |
Output is correct |
24 |
Correct |
10 ms |
1084 KB |
Output is correct |
25 |
Correct |
15 ms |
1112 KB |
Output is correct |
26 |
Correct |
10 ms |
1116 KB |
Output is correct |
27 |
Correct |
11 ms |
1016 KB |
Output is correct |
28 |
Correct |
10 ms |
1172 KB |
Output is correct |
29 |
Correct |
841 ms |
52528 KB |
Output is correct |
30 |
Correct |
907 ms |
51204 KB |
Output is correct |
31 |
Correct |
1026 ms |
55124 KB |
Output is correct |
32 |
Correct |
712 ms |
55124 KB |
Output is correct |
33 |
Correct |
696 ms |
48044 KB |
Output is correct |
34 |
Correct |
920 ms |
55000 KB |
Output is correct |
35 |
Correct |
1126 ms |
70596 KB |
Output is correct |
36 |
Correct |
839 ms |
61776 KB |
Output is correct |
37 |
Correct |
1406 ms |
71068 KB |
Output is correct |