#include <bits/stdc++.h>
using namespace std;
int n, m;
const int MXN = 2005;
int arr[MXN][MXN];
int mn = INT_MAX, mx = INT_MIN, ans;
int pfmax[MXN][MXN];
int sfmin[MXN][MXN];
bool works(int k) {
int last_used = INT_MAX;
for (int i=1; i<=n; i++) {
int pos = 0;
while (pos + 1 <= m && arr[i][pos+1] <= mn + k) {
pos++;
}
pos = min(pos, last_used);
last_used = pos;
if (sfmin[i][pos+1] < mx - k) {
return false;
}
}
return true;
}
void prep() {
for (int i=1; i<=n; i++) {
pfmax[i][0] = INT_MIN;
pfmax[i][1] = arr[i][1];
for (int j=2; j<=m; j++) {
pfmax[i][j] = max(pfmax[i][j-1], arr[i][j]);
}
sfmin[i][m+1] = INT_MAX;
sfmin[i][m] = arr[i][m];
for (int j=m-1; j>=1; j--) {
sfmin[i][j] = min(sfmin[i][j+1], arr[i][j]);
}
}
}
int copytemp[MXN][MXN];
void rot() {
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
copytemp[j][n-i+1] = arr[i][j];
}
}
swap(n, m);
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
arr[i][j] = copytemp[i][j];
}
}
}
signed main() {
cin >> n >> m;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
cin >> arr[i][j];
mn = min(mn, arr[i][j]);
mx = max(mx, arr[i][j]);
}
}
ans = mx - mn;
for (int X=0; X<4; X++) {
rot();
prep();
int low = 0;
int high = INT_MAX / 2;
while (low + 1 < high) {
int mid = (low + high) / 2;
if (works(mid)) {
high = mid;
}
else {
low = mid;
}
}
if (works(low)) {
ans = min(ans, low);
}
else {
ans = min(ans, high);
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
3584 KB |
Output is correct |
16 |
Correct |
16 ms |
4224 KB |
Output is correct |
17 |
Correct |
31 ms |
4364 KB |
Output is correct |
18 |
Correct |
31 ms |
4476 KB |
Output is correct |
19 |
Correct |
33 ms |
4472 KB |
Output is correct |
20 |
Correct |
30 ms |
4352 KB |
Output is correct |
21 |
Correct |
39 ms |
4472 KB |
Output is correct |
22 |
Correct |
45 ms |
4472 KB |
Output is correct |
23 |
Correct |
40 ms |
4472 KB |
Output is correct |
24 |
Correct |
36 ms |
4488 KB |
Output is correct |
25 |
Correct |
41 ms |
4472 KB |
Output is correct |
26 |
Correct |
39 ms |
4472 KB |
Output is correct |
27 |
Correct |
41 ms |
4472 KB |
Output is correct |
28 |
Correct |
42 ms |
4504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
3584 KB |
Output is correct |
16 |
Correct |
16 ms |
4224 KB |
Output is correct |
17 |
Correct |
31 ms |
4364 KB |
Output is correct |
18 |
Correct |
31 ms |
4476 KB |
Output is correct |
19 |
Correct |
33 ms |
4472 KB |
Output is correct |
20 |
Correct |
30 ms |
4352 KB |
Output is correct |
21 |
Correct |
39 ms |
4472 KB |
Output is correct |
22 |
Correct |
45 ms |
4472 KB |
Output is correct |
23 |
Correct |
40 ms |
4472 KB |
Output is correct |
24 |
Correct |
36 ms |
4488 KB |
Output is correct |
25 |
Correct |
41 ms |
4472 KB |
Output is correct |
26 |
Correct |
39 ms |
4472 KB |
Output is correct |
27 |
Correct |
41 ms |
4472 KB |
Output is correct |
28 |
Correct |
42 ms |
4504 KB |
Output is correct |
29 |
Correct |
2668 ms |
64428 KB |
Output is correct |
30 |
Correct |
2540 ms |
64524 KB |
Output is correct |
31 |
Correct |
2766 ms |
64628 KB |
Output is correct |
32 |
Correct |
2704 ms |
64320 KB |
Output is correct |
33 |
Correct |
2374 ms |
64788 KB |
Output is correct |
34 |
Correct |
2728 ms |
64480 KB |
Output is correct |
35 |
Correct |
3812 ms |
65208 KB |
Output is correct |
36 |
Correct |
3341 ms |
64476 KB |
Output is correct |
37 |
Correct |
3919 ms |
66020 KB |
Output is correct |