This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
int lo[2001][2001], hi[2001][2001];
int calcLo[mn], calcHi[mn];
vector<int> row[mn], col[mn];
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 (int i = 0; i < row[pos].size(); i++) {
int r = row[pos][i], c = col[pos][i];
curLo = min(curLo, lo[r][c]);
curHi = max(curHi, hi[r][c]);
}
}
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 (vector<vector<int>> &vec, 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[i][j]));
hi[i][j] = max(hi[i][j - 1], hiCol = max(hiCol, vec[i][j]));
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int cur = getID(vec[i][j]);
row[cur].push_back(i);
col[cur].push_back(j);
}
}
int curLo = INT_MAX, curHi = INT_MIN;
for (int k = cmp.size() - 1; k >= 0; k--) {
calcLo[k] = curLo, calcHi[k] = curHi;
for (int i = 0; i < row[k].size(); i++) {
int r = row[k][i], c = col[k][i];
curLo = min(curLo, lo[r][c]);
curHi = max(curHi, hi[r][c]);
}
}
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++) {
row[k].clear();
col[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 (stderr)
joioi.cpp: In function 'bool check(int)':
joioi.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int k = 0; k < cmp.size(); k++) {
| ~~^~~~~~~~~~~~
joioi.cpp:27:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | while (pos + 1 < cmp.size() && cmp[pos + 1] < cmp[k] - delta) {
| ~~~~~~~~^~~~~~~~~~~~
joioi.cpp:29:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 0; i < row[pos].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
joioi.cpp: In function 'int solve(std::vector<std::vector<int> >&, int, int)':
joioi.cpp:64:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < row[k].size(); i++) {
| ~~^~~~~~~~~~~~~~~
joioi.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int k = 0; k < cmp.size(); k++) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |