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;
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> (m+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];
};
pair <int,int> start;
int mx = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++){
cin >> g[i][j];
if(g[i][j] > mx) {
mx = g[i][j];
start = {i, j};
}
}
}
int ans = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
if(can(g[i][j], mx, start, {i, j})) {
ans = max(mx - g[i][j] - can(g[i][j], mx, start, {i, j}), ans);
}
}
}
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... |