#include <bits/stdc++.h>
using namespace std;
const int N = 2001;
int prefix[N], suffix[N], diff[N][N], a[N][N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int maxi = INT_MIN, mini = INT_MAX;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
maxi = max(maxi, a[i][j]);
mini = min(mini, a[i][j]);
}
}
fill(diff[0], diff[0] + m + 1, INT_MIN);
int ans = INT_MAX;
for (int k = 0; k < 2; ++k) {
for (int i = 0; i < n; ++i) {
reverse(a[i], a[i] + m);
}
for (int l = 0; l < 2; ++l) {
reverse(a, a + n);
for (int i = 0; i < n; ++i) {
copy(a[i], a[i] + m, prefix);
for (int j = 1; j < m; ++j) {
prefix[j] = min(prefix[j], prefix[j - 1]);
}
copy(a[i], a[i] + m, suffix);
for (int j = m - 2; j >= 0; --j) {
suffix[j] = max(suffix[j], suffix[j + 1]);
}
for (int j = 0; j <= m; ++j) {
diff[i + 1][j] = max({
j > 0 ? maxi - prefix[j - 1] : INT_MIN,
j < m ? suffix[j] - mini : INT_MIN,
diff[i][j]
});
if (j > 0) {
diff[i + 1][j] = min(diff[i + 1][j], diff[i + 1][j - 1]);
}
}
}
ans = min(ans, *min_element(diff[n], diff[n] + m + 1));
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
7 ms |
2896 KB |
Output is correct |
17 |
Correct |
9 ms |
3136 KB |
Output is correct |
18 |
Correct |
9 ms |
3148 KB |
Output is correct |
19 |
Correct |
10 ms |
3020 KB |
Output is correct |
20 |
Correct |
9 ms |
2772 KB |
Output is correct |
21 |
Correct |
10 ms |
3252 KB |
Output is correct |
22 |
Correct |
9 ms |
3220 KB |
Output is correct |
23 |
Correct |
10 ms |
3148 KB |
Output is correct |
24 |
Correct |
8 ms |
2920 KB |
Output is correct |
25 |
Correct |
9 ms |
3148 KB |
Output is correct |
26 |
Correct |
9 ms |
3148 KB |
Output is correct |
27 |
Correct |
9 ms |
3148 KB |
Output is correct |
28 |
Correct |
9 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
7 ms |
2896 KB |
Output is correct |
17 |
Correct |
9 ms |
3136 KB |
Output is correct |
18 |
Correct |
9 ms |
3148 KB |
Output is correct |
19 |
Correct |
10 ms |
3020 KB |
Output is correct |
20 |
Correct |
9 ms |
2772 KB |
Output is correct |
21 |
Correct |
10 ms |
3252 KB |
Output is correct |
22 |
Correct |
9 ms |
3220 KB |
Output is correct |
23 |
Correct |
10 ms |
3148 KB |
Output is correct |
24 |
Correct |
8 ms |
2920 KB |
Output is correct |
25 |
Correct |
9 ms |
3148 KB |
Output is correct |
26 |
Correct |
9 ms |
3148 KB |
Output is correct |
27 |
Correct |
9 ms |
3148 KB |
Output is correct |
28 |
Correct |
9 ms |
3148 KB |
Output is correct |
29 |
Correct |
547 ms |
30420 KB |
Output is correct |
30 |
Correct |
571 ms |
32028 KB |
Output is correct |
31 |
Correct |
575 ms |
32008 KB |
Output is correct |
32 |
Correct |
594 ms |
31940 KB |
Output is correct |
33 |
Correct |
500 ms |
28116 KB |
Output is correct |
34 |
Correct |
582 ms |
31948 KB |
Output is correct |
35 |
Correct |
731 ms |
44144 KB |
Output is correct |
36 |
Correct |
596 ms |
32844 KB |
Output is correct |
37 |
Correct |
678 ms |
33380 KB |
Output is correct |