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;
int dis(pair <int,int>a, pair <int,int> b) {
return abs(a.first - b.first) + abs(a.second - b.second);
}
int main(void) {
int n, m;
cin >> n >> m;
assert(n != 2 and m != 2);
if(n == 1) {
vector <int> a(m);
for(int i = 0; i < m; i ++) cin >> a[i];
int ans = INT_MIN;
for(int i = 0; i < m; i ++) {
for(int end = i; end < m; end ++) {
ans = max(ans, *max_element(a.begin() + i, a.begin() + end) - *min_element(a.begin() + i, a.begin() + end) - (end - i));
// cout << *max_element(a.begin() + i, a.begin() + end) << " " << *min_element(a.begin() + i, a.begin() + end) << endl;
}
}
cout << ans;
return false;
}
vector <vector <int> > g(n+2, vector <int> (m+2, -1));
vector <vector <int> > used(n+2, vector <int> (m+2, 0));
vector <vector <int> > dis(n+2, vector <int> (n+2, 0));
queue <pair <int,int>> q;
int it[] = {0, 0, +1, -1};
int jt[] = {+1, -1, 0, 0};
function <int(int, int, pair <int,int>, pair <int,int>)> can=[&](int mn, int mx, pair <int,int> s, pair <int,int> e) {
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) used[i][j] = dis[i][j] = 0;
}
q.push(s);
used[s.first][s.second] = 1;
dis[s.first][s.second] = 1;
while(!q.empty()) {
pair <int,int> v = q.front();
q.pop();
for(int to = 0; to < 4; to ++) {
int ni = v.first + it[to];
int nj = v.second + jt[to];
if(!used[ni][nj] and g[ni][nj] != -1 and g[ni][nj] >= mn and g[ni][nj] <= mx) {
q.push({ni, nj});
used[ni][nj] = 1;
dis[ni][nj] = dis[v.first][v.second] + 1;
}
}
}
return dis[e.first][e.second];
};
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) cin >> g[i][j];
}
int ans = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
for(int k = i+1; k <= n; k ++) {
for(int l = j+1; l <= m; l ++) {
if(can(min(g[i][j], g[k][l]), max(g[i][j], g[k][l]), {i, j}, {k, l})) {
ans = max(ans, max(g[i][j], g[k][l])
- min(g[i][j], g[k][l]) - can(min(g[i][j], g[k][l]), max(g[i][j], g[k][l]), {i, j}, {k, l}));
}
}
}
}
}
cout << ans;
return false;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |