#include<bits/stdc++.h>
using namespace std;
#define int long long
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
const int N = 2007, INF = 1e9 + 7;
int n, m, mn = INF, a[N][N], r[N];
int suffmn[N][N], suffmx[N][N];
bool check_(int mid) {
for (int i = 0; i < n; ++i) {
suffmn[i][m] = INF; for (int j = m - 1; j >= 0; --j) suffmn[i][j] = min(suffmn[i][j + 1], a[i][j]);
suffmx[i][m] = -INF; for (int j = m - 1; j >= 0; --j) suffmx[i][j] = max(suffmx[i][j + 1], a[i][j]);
}
int mx = mn + mid;
for (int i = 0; i < n; ++i) { r[i] = 0; while (r[i] < m && a[i][r[i]] <= mx) ++r[i]; }
int MN, MX, c;
c = m; MN = INF; MX = -INF;
for (int i = 0; i < n; ++i) { c = min(c, r[i]); MN = min(MN, suffmn[i][c]); MX = max(MX, suffmx[i][c]); }
if (MX - MN <= mid) return 1;
c = m; MN = INF; MX = -INF;
for (int i = n - 1; i >= 0; --i) { c = min(c, r[i]); MN = min(MN, suffmn[i][c]); MX = max(MX, suffmx[i][c]); }
if (MX - MN <= mid) return 1;
return 0;
}
void kek() {
for (int i = 0; i < n; ++i) for (int j = 0; j < m / 2; ++j) swap(a[i][j], a[i][m - 1 - j]);
}
bool check(int mid) {
if (check_(mid)) return 1;
kek(); bool ans = check_(mid); kek();
return ans;
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> m;
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { cin >> a[i][j]; mn = min(mn, a[i][j]); }
int l = -1, r = INF;
while (l < r - 1) { int mid = (l + r) >> 1; if (check(mid)) r = mid; else l = mid; }
cout << r << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
15 ms |
3704 KB |
Output is correct |
17 |
Correct |
21 ms |
3960 KB |
Output is correct |
18 |
Correct |
21 ms |
3960 KB |
Output is correct |
19 |
Correct |
24 ms |
3960 KB |
Output is correct |
20 |
Correct |
21 ms |
3576 KB |
Output is correct |
21 |
Correct |
29 ms |
4088 KB |
Output is correct |
22 |
Correct |
27 ms |
4088 KB |
Output is correct |
23 |
Correct |
27 ms |
4088 KB |
Output is correct |
24 |
Correct |
21 ms |
3704 KB |
Output is correct |
25 |
Correct |
28 ms |
4088 KB |
Output is correct |
26 |
Correct |
24 ms |
4088 KB |
Output is correct |
27 |
Correct |
25 ms |
4088 KB |
Output is correct |
28 |
Correct |
25 ms |
4088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
15 ms |
3704 KB |
Output is correct |
17 |
Correct |
21 ms |
3960 KB |
Output is correct |
18 |
Correct |
21 ms |
3960 KB |
Output is correct |
19 |
Correct |
24 ms |
3960 KB |
Output is correct |
20 |
Correct |
21 ms |
3576 KB |
Output is correct |
21 |
Correct |
29 ms |
4088 KB |
Output is correct |
22 |
Correct |
27 ms |
4088 KB |
Output is correct |
23 |
Correct |
27 ms |
4088 KB |
Output is correct |
24 |
Correct |
21 ms |
3704 KB |
Output is correct |
25 |
Correct |
28 ms |
4088 KB |
Output is correct |
26 |
Correct |
24 ms |
4088 KB |
Output is correct |
27 |
Correct |
25 ms |
4088 KB |
Output is correct |
28 |
Correct |
25 ms |
4088 KB |
Output is correct |
29 |
Correct |
1800 ms |
112048 KB |
Output is correct |
30 |
Correct |
1904 ms |
116276 KB |
Output is correct |
31 |
Correct |
1681 ms |
117756 KB |
Output is correct |
32 |
Correct |
2049 ms |
117716 KB |
Output is correct |
33 |
Correct |
1571 ms |
103224 KB |
Output is correct |
34 |
Correct |
2040 ms |
117880 KB |
Output is correct |
35 |
Correct |
2191 ms |
133528 KB |
Output is correct |
36 |
Correct |
2304 ms |
127732 KB |
Output is correct |
37 |
Correct |
2584 ms |
133368 KB |
Output is correct |