#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())
vector<int> cmp;
int getID (int targ) {
return lower_bound(all(cmp), targ) - cmp.begin();
}
const int mn = 4e6 + 6;
int lo[2002][2002], hi[2002][2002], loCol[2002][2002], hiCol[2002][2002], hiRow[2002][2002];
int calcLo[mn], calcHi[mn], bound[2002];
vector<pii> contain[mn];
bool ok (int i, int j) {
return j < bound[i];
}
bool check (int delta) {
int pos = -1, curLo = INT_MAX, curHi = INT_MIN;
for (int k = 0; k < cmp.size(); k++) {
while (pos + 1 < cmp.size() && cmp[pos + 1] < cmp[k] - delta) {
pos++;
for (auto [row, col] : contain[pos]) {
curLo = min(curLo, lo[row][col]);
curHi = max(curHi, hi[row][col]);
}
}
int cHi = max(calcHi[k], curHi);
int cLo = min(calcLo[k], curLo);
if (cHi != INT_MIN && cLo != INT_MAX && cHi - cLo <= delta) return 1;
}
return 0;
}
int solve (vector<vector<int>> &vec, int n, int m) {
for (int j = 1; j <= m; j++) {
int curLo = INT_MAX, curHi = INT_MIN;
for (int i = n; i >= 1; i--) {
loCol[i][j] = curLo = min(curLo, vec[i][j]);
hiCol[i][j] = curHi = max(curHi, vec[i][j]);
}
}
for (int i = 1; i <= n; i++) {
int curLo = INT_MAX, curHi = INT_MIN;
//lo[i][0] = curLo, hi[i][0] = curHi;
for (int j = 1; j <= m; j++) {
lo[i][j] = curLo = min(curLo, loCol[i][j]);
hi[i][j] = curHi = max(curHi, hiCol[i][j]);
}
curHi = INT_MIN;
for (int j = m; j >= 1; j--)
hiRow[i][j] = curHi = max(curHi, vec[i][j]);
}
priority_queue<tt> pqLo, pqHi;
for (int i = 1; i <= n; i++) {
bound[i] = m + 1;
for (int j = 1; j <= m; j++) {
pqLo.push(make_tuple(-lo[i][j], i, j));
pqHi.push(make_tuple( hi[i][j], i, j));
contain[getID(vec[i][j])].push_back({i, j});
}
}
for (int k = 0; k < cmp.size(); k++) {
for (auto [row, col] : contain[k])
while (bound[row] > 1 && hiRow[row][bound[row] - 1] <= cmp[k]) bound[row]--;
while (pqLo.size()) {
int val, i, j; tie(val, i, j) = pqLo.top();
if (ok(i, j)) break;
else pqLo.pop();
}
while (pqHi.size()) {
int val, i, j; tie(val, i, j) = pqHi.top();
if (ok(i, j)) break;
else pqHi.pop();
}
calcLo[k] = (pqLo.size() ? -get<0>(pqLo.top()) : INT_MAX);
calcHi[k] = (pqHi.size() ? get<0>(pqHi.top()) : INT_MIN);
}
int ans = 0;
if (!check(0)) {
for (int i = 29; i >= 0; i--) {
int mask = ans | (1 << i);
if (!check(mask)) ans |= (1 << i);
}
ans++;
}
for (int k = 0; k < cmp.size(); k++)
contain[k].clear();
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<int>> vec(n + 1, vector<int>(m + 1));
vector<vector<int>> rot(m + 1, vector<int>(n + 1));
//cmp.push_back(0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> vec[i][j];
rot[j][n - i + 1] = vec[i][j];
cmp.push_back(vec[i][j]);
}
}
sort(all(cmp)); filter(cmp);
cout << min(solve(vec, n, m), solve(rot, m, n));
return 0;
}
Compilation message
joioi.cpp: In function 'bool check(int)':
joioi.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int k = 0; k < cmp.size(); k++) {
| ~~^~~~~~~~~~~~
joioi.cpp:31:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | while (pos + 1 < cmp.size() && cmp[pos + 1] < cmp[k] - delta) {
| ~~~~~~~~^~~~~~~~~~~~
joioi.cpp:33:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for (auto [row, col] : contain[pos]) {
| ^
joioi.cpp: In function 'int solve(std::vector<std::vector<int> >&, int, int)':
joioi.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int k = 0; k < cmp.size(); k++) {
| ~~^~~~~~~~~~~~
joioi.cpp:77:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | for (auto [row, col] : contain[k])
| ^
joioi.cpp:103:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int k = 0; k < cmp.size(); k++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
107096 KB |
Output is correct |
2 |
Correct |
18 ms |
107096 KB |
Output is correct |
3 |
Correct |
18 ms |
107356 KB |
Output is correct |
4 |
Correct |
18 ms |
107172 KB |
Output is correct |
5 |
Correct |
17 ms |
107356 KB |
Output is correct |
6 |
Correct |
16 ms |
107188 KB |
Output is correct |
7 |
Correct |
17 ms |
107096 KB |
Output is correct |
8 |
Correct |
16 ms |
107356 KB |
Output is correct |
9 |
Correct |
19 ms |
107356 KB |
Output is correct |
10 |
Correct |
18 ms |
107288 KB |
Output is correct |
11 |
Correct |
18 ms |
107356 KB |
Output is correct |
12 |
Correct |
17 ms |
107356 KB |
Output is correct |
13 |
Correct |
16 ms |
107352 KB |
Output is correct |
14 |
Correct |
17 ms |
107356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
107096 KB |
Output is correct |
2 |
Correct |
18 ms |
107096 KB |
Output is correct |
3 |
Correct |
18 ms |
107356 KB |
Output is correct |
4 |
Correct |
18 ms |
107172 KB |
Output is correct |
5 |
Correct |
17 ms |
107356 KB |
Output is correct |
6 |
Correct |
16 ms |
107188 KB |
Output is correct |
7 |
Correct |
17 ms |
107096 KB |
Output is correct |
8 |
Correct |
16 ms |
107356 KB |
Output is correct |
9 |
Correct |
19 ms |
107356 KB |
Output is correct |
10 |
Correct |
18 ms |
107288 KB |
Output is correct |
11 |
Correct |
18 ms |
107356 KB |
Output is correct |
12 |
Correct |
17 ms |
107356 KB |
Output is correct |
13 |
Correct |
16 ms |
107352 KB |
Output is correct |
14 |
Correct |
17 ms |
107356 KB |
Output is correct |
15 |
Correct |
20 ms |
117596 KB |
Output is correct |
16 |
Correct |
40 ms |
120136 KB |
Output is correct |
17 |
Correct |
60 ms |
121236 KB |
Output is correct |
18 |
Correct |
58 ms |
121236 KB |
Output is correct |
19 |
Correct |
64 ms |
121260 KB |
Output is correct |
20 |
Correct |
52 ms |
120840 KB |
Output is correct |
21 |
Correct |
61 ms |
121152 KB |
Output is correct |
22 |
Correct |
64 ms |
121236 KB |
Output is correct |
23 |
Correct |
60 ms |
121232 KB |
Output is correct |
24 |
Correct |
57 ms |
120956 KB |
Output is correct |
25 |
Correct |
62 ms |
121232 KB |
Output is correct |
26 |
Correct |
67 ms |
121236 KB |
Output is correct |
27 |
Correct |
61 ms |
121232 KB |
Output is correct |
28 |
Correct |
61 ms |
121228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
107096 KB |
Output is correct |
2 |
Correct |
18 ms |
107096 KB |
Output is correct |
3 |
Correct |
18 ms |
107356 KB |
Output is correct |
4 |
Correct |
18 ms |
107172 KB |
Output is correct |
5 |
Correct |
17 ms |
107356 KB |
Output is correct |
6 |
Correct |
16 ms |
107188 KB |
Output is correct |
7 |
Correct |
17 ms |
107096 KB |
Output is correct |
8 |
Correct |
16 ms |
107356 KB |
Output is correct |
9 |
Correct |
19 ms |
107356 KB |
Output is correct |
10 |
Correct |
18 ms |
107288 KB |
Output is correct |
11 |
Correct |
18 ms |
107356 KB |
Output is correct |
12 |
Correct |
17 ms |
107356 KB |
Output is correct |
13 |
Correct |
16 ms |
107352 KB |
Output is correct |
14 |
Correct |
17 ms |
107356 KB |
Output is correct |
15 |
Correct |
20 ms |
117596 KB |
Output is correct |
16 |
Correct |
40 ms |
120136 KB |
Output is correct |
17 |
Correct |
60 ms |
121236 KB |
Output is correct |
18 |
Correct |
58 ms |
121236 KB |
Output is correct |
19 |
Correct |
64 ms |
121260 KB |
Output is correct |
20 |
Correct |
52 ms |
120840 KB |
Output is correct |
21 |
Correct |
61 ms |
121152 KB |
Output is correct |
22 |
Correct |
64 ms |
121236 KB |
Output is correct |
23 |
Correct |
60 ms |
121232 KB |
Output is correct |
24 |
Correct |
57 ms |
120956 KB |
Output is correct |
25 |
Correct |
62 ms |
121232 KB |
Output is correct |
26 |
Correct |
67 ms |
121236 KB |
Output is correct |
27 |
Correct |
61 ms |
121232 KB |
Output is correct |
28 |
Correct |
61 ms |
121228 KB |
Output is correct |
29 |
Runtime error |
756 ms |
262144 KB |
Execution killed with signal 9 |
30 |
Halted |
0 ms |
0 KB |
- |