// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
int d[123];
long long read() {
char ch = getchar();
while (ch < '0' || ch > '9') {
ch = getchar();
}
long long v = 0;
while ('0' <= ch && ch <= '9') {
v = v * 10 + (int) (ch - '0');
ch = getchar();
}
return v;
}
void write(int x) {
int len = 0;
while (x > 0) {
d[len++] = x % 10;
x /= 10;
}
for (int i = len - 1; i >= 0; i--) {
putchar('0' + d[i]);
}
if (len == 0) {
putchar('0');
}
putchar('\n');
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int N = read(), M = read();
vector<vector<int>> A(N, vector<int>(M)); for(auto& v : A) for(auto& x : v) x = read();
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];
}
write(ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
9 ms |
1644 KB |
Output is correct |
17 |
Correct |
12 ms |
1776 KB |
Output is correct |
18 |
Correct |
11 ms |
1772 KB |
Output is correct |
19 |
Correct |
12 ms |
1776 KB |
Output is correct |
20 |
Correct |
10 ms |
1772 KB |
Output is correct |
21 |
Correct |
13 ms |
1904 KB |
Output is correct |
22 |
Correct |
12 ms |
1896 KB |
Output is correct |
23 |
Correct |
13 ms |
1896 KB |
Output is correct |
24 |
Correct |
11 ms |
1900 KB |
Output is correct |
25 |
Correct |
12 ms |
1900 KB |
Output is correct |
26 |
Correct |
12 ms |
1900 KB |
Output is correct |
27 |
Correct |
12 ms |
1896 KB |
Output is correct |
28 |
Correct |
12 ms |
1900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
9 ms |
1644 KB |
Output is correct |
17 |
Correct |
12 ms |
1776 KB |
Output is correct |
18 |
Correct |
11 ms |
1772 KB |
Output is correct |
19 |
Correct |
12 ms |
1776 KB |
Output is correct |
20 |
Correct |
10 ms |
1772 KB |
Output is correct |
21 |
Correct |
13 ms |
1904 KB |
Output is correct |
22 |
Correct |
12 ms |
1896 KB |
Output is correct |
23 |
Correct |
13 ms |
1896 KB |
Output is correct |
24 |
Correct |
11 ms |
1900 KB |
Output is correct |
25 |
Correct |
12 ms |
1900 KB |
Output is correct |
26 |
Correct |
12 ms |
1900 KB |
Output is correct |
27 |
Correct |
12 ms |
1896 KB |
Output is correct |
28 |
Correct |
12 ms |
1900 KB |
Output is correct |
29 |
Correct |
1489 ms |
92972 KB |
Output is correct |
30 |
Correct |
1409 ms |
92384 KB |
Output is correct |
31 |
Correct |
1547 ms |
99184 KB |
Output is correct |
32 |
Correct |
1531 ms |
99360 KB |
Output is correct |
33 |
Correct |
1273 ms |
98340 KB |
Output is correct |
34 |
Correct |
1554 ms |
99304 KB |
Output is correct |
35 |
Correct |
1784 ms |
118124 KB |
Output is correct |
36 |
Correct |
1481 ms |
103152 KB |
Output is correct |
37 |
Correct |
1747 ms |
119320 KB |
Output is correct |