#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
#define INF (int)(1e18)
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 2010;
int a[MAXN][MAXN];
int tmp[MAXN][MAXN];
int r[MAXN];
int l[MAXN];
// rotate CCW once
void rot(int &n, int &m) {
For(i, 0, n - 1) For(j, 0, m - 1) {
tmp[m - j - 1][i] = a[i][j];
}
swap(n, m);
For(i, 0, n - 1) For(j, 0, m - 1) {
a[i][j] = tmp[i][j];
}
}
void check1(int n, int m, int lo) {
For(i, 0, n - 1) {
r[i] = -1;
for(; r[i] + 1 < m && a[i][r[i] + 1] >= lo; r[i]++);
if(i) r[i] = min(r[i], r[i - 1]);
}
}
void check2(int n, int m, int hi) {
Forr(i, n - 1, 0) {
l[i] = m;
for(; l[i] - 1 >= 0 && a[i][l[i] - 1] <= hi; l[i]--);
if(i < n - 1) l[i] = max(l[i], l[i + 1]);
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
// =^-w-^=
int n, m; cin >> n >> m;
int mx = -1, mn = INF;
For(i, 0, n - 1) For(j, 0, m - 1) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
int ans = INF;
For(_, 0, 3) {
int hi = mx - mn;
int lo = -1;
while(hi - lo > 1) {
int mi = (hi + lo) / 2;
check1(n, m, mx - mi);
check2(n, m, mn + mi);
bool ok = true;
For(i, 0, n - 1) {
if(r[i] + 1 < l[i]) {
ok = false;
break;
}
}
if(ok) hi = mi;
else lo = mi;
}
ans = min(ans, hi);
rot(n, m);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
2004 KB |
Output is correct |
16 |
Correct |
5 ms |
2644 KB |
Output is correct |
17 |
Correct |
9 ms |
2772 KB |
Output is correct |
18 |
Correct |
10 ms |
2800 KB |
Output is correct |
19 |
Correct |
9 ms |
2772 KB |
Output is correct |
20 |
Correct |
9 ms |
2780 KB |
Output is correct |
21 |
Correct |
11 ms |
2928 KB |
Output is correct |
22 |
Correct |
12 ms |
2900 KB |
Output is correct |
23 |
Correct |
10 ms |
2872 KB |
Output is correct |
24 |
Correct |
9 ms |
2900 KB |
Output is correct |
25 |
Correct |
11 ms |
2856 KB |
Output is correct |
26 |
Correct |
11 ms |
2956 KB |
Output is correct |
27 |
Correct |
11 ms |
2912 KB |
Output is correct |
28 |
Correct |
12 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
2004 KB |
Output is correct |
16 |
Correct |
5 ms |
2644 KB |
Output is correct |
17 |
Correct |
9 ms |
2772 KB |
Output is correct |
18 |
Correct |
10 ms |
2800 KB |
Output is correct |
19 |
Correct |
9 ms |
2772 KB |
Output is correct |
20 |
Correct |
9 ms |
2780 KB |
Output is correct |
21 |
Correct |
11 ms |
2928 KB |
Output is correct |
22 |
Correct |
12 ms |
2900 KB |
Output is correct |
23 |
Correct |
10 ms |
2872 KB |
Output is correct |
24 |
Correct |
9 ms |
2900 KB |
Output is correct |
25 |
Correct |
11 ms |
2856 KB |
Output is correct |
26 |
Correct |
11 ms |
2956 KB |
Output is correct |
27 |
Correct |
11 ms |
2912 KB |
Output is correct |
28 |
Correct |
12 ms |
2900 KB |
Output is correct |
29 |
Correct |
617 ms |
85480 KB |
Output is correct |
30 |
Correct |
598 ms |
84916 KB |
Output is correct |
31 |
Correct |
617 ms |
86336 KB |
Output is correct |
32 |
Correct |
626 ms |
86392 KB |
Output is correct |
33 |
Correct |
570 ms |
82860 KB |
Output is correct |
34 |
Correct |
639 ms |
86612 KB |
Output is correct |
35 |
Correct |
960 ms |
102200 KB |
Output is correct |
36 |
Correct |
860 ms |
96628 KB |
Output is correct |
37 |
Correct |
989 ms |
102372 KB |
Output is correct |