#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 get (int arr, int it) {
return vec[arr][it];
}
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 = get(arr, coor[k]);
while (pos + 1 < prod && get(arr, coor[pos + 1]) < cur - delta) {
pos++;
curLo = min(curLo, lo[coor[pos]]);
curHi = max(curHi, hi[coor[pos]]);
}
if (k + 1 < prod && get(arr, coor[k]) == get(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 get(arr, a) < get(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 main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
prod = n * m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> vec[0][getID(i, j)];
vec[1][getID(j, n - i + 1)] = vec[0][getID(i, j)];
}
}
cout << min(solve(0, n, m), solve(1, m, n));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12748 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 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 |
12752 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12776 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12748 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 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 |
12752 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12776 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
12 ms |
19036 KB |
Output is correct |
17 |
Correct |
32 ms |
19292 KB |
Output is correct |
18 |
Correct |
21 ms |
19292 KB |
Output is correct |
19 |
Correct |
25 ms |
19288 KB |
Output is correct |
20 |
Correct |
20 ms |
19292 KB |
Output is correct |
21 |
Correct |
23 ms |
19300 KB |
Output is correct |
22 |
Correct |
27 ms |
19288 KB |
Output is correct |
23 |
Correct |
29 ms |
19272 KB |
Output is correct |
24 |
Correct |
22 ms |
19292 KB |
Output is correct |
25 |
Correct |
24 ms |
19448 KB |
Output is correct |
26 |
Correct |
24 ms |
19400 KB |
Output is correct |
27 |
Correct |
24 ms |
19476 KB |
Output is correct |
28 |
Correct |
25 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12748 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 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 |
12752 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12776 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
12 ms |
19036 KB |
Output is correct |
17 |
Correct |
32 ms |
19292 KB |
Output is correct |
18 |
Correct |
21 ms |
19292 KB |
Output is correct |
19 |
Correct |
25 ms |
19288 KB |
Output is correct |
20 |
Correct |
20 ms |
19292 KB |
Output is correct |
21 |
Correct |
23 ms |
19300 KB |
Output is correct |
22 |
Correct |
27 ms |
19288 KB |
Output is correct |
23 |
Correct |
29 ms |
19272 KB |
Output is correct |
24 |
Correct |
22 ms |
19292 KB |
Output is correct |
25 |
Correct |
24 ms |
19448 KB |
Output is correct |
26 |
Correct |
24 ms |
19400 KB |
Output is correct |
27 |
Correct |
24 ms |
19476 KB |
Output is correct |
28 |
Correct |
25 ms |
19292 KB |
Output is correct |
29 |
Correct |
3056 ms |
121864 KB |
Output is correct |
30 |
Correct |
3143 ms |
121344 KB |
Output is correct |
31 |
Correct |
3538 ms |
121940 KB |
Output is correct |
32 |
Correct |
3271 ms |
127796 KB |
Output is correct |
33 |
Correct |
2629 ms |
125340 KB |
Output is correct |
34 |
Correct |
3451 ms |
127796 KB |
Output is correct |
35 |
Execution timed out |
4041 ms |
148576 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |