#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 = 4e6;
int lo[2001][2001], hi[2001][2001], vec[2][2001][2001];
int calcLo[mn], calcHi[mn], prod;
pii coor[mn];
int get (int arr, pii it) {
return vec[arr][it.first][it.second];
}
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) {
int row, col; tie(row, col) = coor[++pos];
curLo = min(curLo, lo[row][col]);
curHi = max(curHi, hi[row][col]);
}
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[i][0] = INT_MAX, hi[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[i][j] = min(lo[i][j - 1], loCol = min(loCol, vec[arr][i][j]));
hi[i][j] = max(hi[i][j - 1], hiCol = max(hiCol, vec[arr][i][j]));
}
}
int counter = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
coor[counter++] = make_pair(i, j);
function<bool(pii a, pii b)> comp = [&] (pii a, pii 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;
int row, col; tie(row, col) = coor[k];
curLo = min(curLo, lo[row][col]);
curHi = max(curHi, hi[row][col]);
}
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][i][j];
vec[1][j][n - i + 1] = vec[0][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 |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12632 KB |
Output is correct |
11 |
Correct |
1 ms |
12764 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 |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12632 KB |
Output is correct |
11 |
Correct |
1 ms |
12764 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 |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
13 ms |
21084 KB |
Output is correct |
17 |
Correct |
30 ms |
21424 KB |
Output is correct |
18 |
Correct |
26 ms |
21340 KB |
Output is correct |
19 |
Correct |
29 ms |
21680 KB |
Output is correct |
20 |
Correct |
23 ms |
21340 KB |
Output is correct |
21 |
Correct |
28 ms |
21544 KB |
Output is correct |
22 |
Correct |
35 ms |
21328 KB |
Output is correct |
23 |
Correct |
30 ms |
21340 KB |
Output is correct |
24 |
Correct |
26 ms |
21340 KB |
Output is correct |
25 |
Correct |
29 ms |
21336 KB |
Output is correct |
26 |
Correct |
29 ms |
21340 KB |
Output is correct |
27 |
Correct |
28 ms |
21340 KB |
Output is correct |
28 |
Correct |
29 ms |
21340 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 |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12632 KB |
Output is correct |
11 |
Correct |
1 ms |
12764 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 |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
13 ms |
21084 KB |
Output is correct |
17 |
Correct |
30 ms |
21424 KB |
Output is correct |
18 |
Correct |
26 ms |
21340 KB |
Output is correct |
19 |
Correct |
29 ms |
21680 KB |
Output is correct |
20 |
Correct |
23 ms |
21340 KB |
Output is correct |
21 |
Correct |
28 ms |
21544 KB |
Output is correct |
22 |
Correct |
35 ms |
21328 KB |
Output is correct |
23 |
Correct |
30 ms |
21340 KB |
Output is correct |
24 |
Correct |
26 ms |
21340 KB |
Output is correct |
25 |
Correct |
29 ms |
21336 KB |
Output is correct |
26 |
Correct |
29 ms |
21340 KB |
Output is correct |
27 |
Correct |
28 ms |
21340 KB |
Output is correct |
28 |
Correct |
29 ms |
21340 KB |
Output is correct |
29 |
Correct |
3522 ms |
137212 KB |
Output is correct |
30 |
Correct |
3809 ms |
136576 KB |
Output is correct |
31 |
Execution timed out |
4019 ms |
137516 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |