// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M; cin >> N >> M;
vector<vector<int>> A(N, vector<int>(M)); for(auto& v : A) for(auto& x : v) cin >> x;
vector<int> U;
vector<array<int, 3>> I;
for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) {
U.push_back(A[i][j]);
I.push_back({A[i][j], i, j});
}
sort(rbegin(I), rend(I));
sort(begin(U), end(U));
U.erase(unique(begin(U), end(U)), end(U));
auto solve = [&]() {
vector<vector<int>> P(N, vector<int>(M));
vector<int> JOI;
for(auto i : I) P[i[1]][i[2]] = i[0];
for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) {
if (i != 0) P[i][j] = min(P[i][j], P[i-1][j]);
if (j != 0) P[i][j] = min(P[i][j], P[i][j-1]);
}
JOI = U;
int ans = int(1e9);
int mn = ans, mx = -ans;
for(int i = 0; i < int(size(I)); i++) {
int j = i;
while(j < int(size(I)) && I[i][0] == I[j][0]) {
int r = I[j][1], c = I[j][2];
mn = min(mn, P[r][c]);
j++;
}
mx = max(mx, I[i][0]);
JOI.pop_back();
if (size(JOI) == 0) break;
// cout << I[i][0] << endl;
// for(auto x : JOI) cout << x << " ";
// cout << endl;
// for(auto x : IOI) cout << x << " ";
// cout << endl;
int res = max(JOI.back() - JOI.front(), mx - mn);
// cout << range(JOI) << " " << range(IOI) << endl;
ans = min(ans, res);
i = j-1;
}
return ans;
};
int ans = int(1e9);
for(int f1 = 0; f1 < 2; f1++) {
for(int f2 = 0; f2 < 2; f2++) {
// cout << 2 * f1 + f2 << nl;
ans = min(ans, solve());
for(auto &i : I) i[2] = M - 1 - i[2];
}
for(auto &i : I) i[1] = N - 1 - i[1];
}
cout << ans << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
12 ms |
1516 KB |
Output is correct |
17 |
Correct |
14 ms |
1520 KB |
Output is correct |
18 |
Correct |
13 ms |
1516 KB |
Output is correct |
19 |
Correct |
13 ms |
1520 KB |
Output is correct |
20 |
Correct |
11 ms |
1516 KB |
Output is correct |
21 |
Correct |
14 ms |
1520 KB |
Output is correct |
22 |
Correct |
16 ms |
1516 KB |
Output is correct |
23 |
Correct |
13 ms |
1520 KB |
Output is correct |
24 |
Correct |
12 ms |
1516 KB |
Output is correct |
25 |
Correct |
15 ms |
1520 KB |
Output is correct |
26 |
Correct |
13 ms |
1520 KB |
Output is correct |
27 |
Correct |
13 ms |
1520 KB |
Output is correct |
28 |
Correct |
14 ms |
1520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
12 ms |
1516 KB |
Output is correct |
17 |
Correct |
14 ms |
1520 KB |
Output is correct |
18 |
Correct |
13 ms |
1516 KB |
Output is correct |
19 |
Correct |
13 ms |
1520 KB |
Output is correct |
20 |
Correct |
11 ms |
1516 KB |
Output is correct |
21 |
Correct |
14 ms |
1520 KB |
Output is correct |
22 |
Correct |
16 ms |
1516 KB |
Output is correct |
23 |
Correct |
13 ms |
1520 KB |
Output is correct |
24 |
Correct |
12 ms |
1516 KB |
Output is correct |
25 |
Correct |
15 ms |
1520 KB |
Output is correct |
26 |
Correct |
13 ms |
1520 KB |
Output is correct |
27 |
Correct |
13 ms |
1520 KB |
Output is correct |
28 |
Correct |
14 ms |
1520 KB |
Output is correct |
29 |
Correct |
1640 ms |
90104 KB |
Output is correct |
30 |
Correct |
1582 ms |
92432 KB |
Output is correct |
31 |
Correct |
1736 ms |
99068 KB |
Output is correct |
32 |
Correct |
1733 ms |
99516 KB |
Output is correct |
33 |
Correct |
1449 ms |
96976 KB |
Output is correct |
34 |
Correct |
1723 ms |
99328 KB |
Output is correct |
35 |
Correct |
2009 ms |
117944 KB |
Output is correct |
36 |
Correct |
1586 ms |
103072 KB |
Output is correct |
37 |
Correct |
1945 ms |
119232 KB |
Output is correct |