#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 4'004'004;
int lo[mn], hi[mn], vec[2][mn], calcLo[mn], calcHi[mn], coor[mn], prod;
int getID (int i, int j) {
return i * 2001 + j;
}
bool check (int arr, int delta) {
int pos = -1, curLo = INT_MAX, curHi = INT_MIN;
for (int k = 0; k < prod; k++) {
int cur = vec[arr][coor[k]];
while (pos + 1 < prod && vec[arr][coor[pos + 1]] < cur - delta) {
pos++;
curLo = min(curLo, lo[coor[pos]]);
curHi = max(curHi, hi[coor[pos]]);
}
if (k + 1 < prod && vec[arr][coor[k]] == vec[arr][coor[k + 1]]) continue;
int cHi = max(calcHi[k], curHi);
int cLo = min(calcLo[k], curLo);
if (cHi == INT_MIN || cHi - cLo <= delta) return 1;
}
return 0;
}
int solve (int arr, int n, int m) {
for (int i = 1; i <= n; i++)
lo[getID(i, 0)] = INT_MAX, hi[getID(i, 0)] = INT_MIN;
for (int j = 1; j <= m; j++) {
int loCol = INT_MAX, hiCol = INT_MIN;
for (int i = n; i >= 1; i--) {
lo[getID(i, j)] = min(lo[getID(i, j - 1)], loCol = min(loCol, vec[arr][getID(i, j)]));
hi[getID(i, j)] = max(hi[getID(i, j - 1)], hiCol = max(hiCol, vec[arr][getID(i, j)]));
}
}
int counter = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
coor[counter++] = getID(i, j);
function<bool(int,int)> comp = [&] (int a, int b) {
return vec[arr][a] < vec[arr][b];
};
sort(coor, coor + prod, comp);
int curLo = INT_MAX, curHi = INT_MIN;
for (int k = prod - 1; k >= 0; k--) {
calcLo[k] = curLo, calcHi[k] = curHi;
curLo = min(curLo, lo[coor[k]]);
curHi = max(curHi, hi[coor[k]]);
}
if (check(arr, 0)) return 0;
int ans = 0;
for (int i = 29; i >= 0; i--)
if (!check(arr, ans | (1 << i))) ans |= (1 << i);
return ans + 1;
}
int getInt() {
int num = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
num = (num << 3) + (num << 1) + (ch - '0');
ch = getchar();
}
return num;
}
int main()
{
int n = getInt(), m = getInt(); prod = n * m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
vec[0][getID(i, j)] = getInt();
vec[1][getID(j, n - i + 1)] = vec[0][getID(i, j)];
}
}
cout << min(solve(0, n, m), solve(1, m, n));
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12632 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12636 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12632 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12636 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
10 ms |
19032 KB |
Output is correct |
17 |
Correct |
19 ms |
19032 KB |
Output is correct |
18 |
Correct |
20 ms |
19060 KB |
Output is correct |
19 |
Correct |
22 ms |
19036 KB |
Output is correct |
20 |
Correct |
19 ms |
19036 KB |
Output is correct |
21 |
Correct |
21 ms |
19036 KB |
Output is correct |
22 |
Correct |
28 ms |
19036 KB |
Output is correct |
23 |
Correct |
23 ms |
19036 KB |
Output is correct |
24 |
Correct |
21 ms |
19032 KB |
Output is correct |
25 |
Correct |
22 ms |
19036 KB |
Output is correct |
26 |
Correct |
22 ms |
19032 KB |
Output is correct |
27 |
Correct |
22 ms |
19036 KB |
Output is correct |
28 |
Correct |
21 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12632 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12636 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
10 ms |
19032 KB |
Output is correct |
17 |
Correct |
19 ms |
19032 KB |
Output is correct |
18 |
Correct |
20 ms |
19060 KB |
Output is correct |
19 |
Correct |
22 ms |
19036 KB |
Output is correct |
20 |
Correct |
19 ms |
19036 KB |
Output is correct |
21 |
Correct |
21 ms |
19036 KB |
Output is correct |
22 |
Correct |
28 ms |
19036 KB |
Output is correct |
23 |
Correct |
23 ms |
19036 KB |
Output is correct |
24 |
Correct |
21 ms |
19032 KB |
Output is correct |
25 |
Correct |
22 ms |
19036 KB |
Output is correct |
26 |
Correct |
22 ms |
19032 KB |
Output is correct |
27 |
Correct |
22 ms |
19036 KB |
Output is correct |
28 |
Correct |
21 ms |
19036 KB |
Output is correct |
29 |
Correct |
2679 ms |
110116 KB |
Output is correct |
30 |
Correct |
2571 ms |
109908 KB |
Output is correct |
31 |
Correct |
2925 ms |
110160 KB |
Output is correct |
32 |
Correct |
2718 ms |
109908 KB |
Output is correct |
33 |
Correct |
2288 ms |
109904 KB |
Output is correct |
34 |
Correct |
2845 ms |
110112 KB |
Output is correct |
35 |
Correct |
3746 ms |
109908 KB |
Output is correct |
36 |
Correct |
3200 ms |
143740 KB |
Output is correct |
37 |
Correct |
3934 ms |
148992 KB |
Output is correct |