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));
int mx = -1e9;
int mn = 1e9;
pair <int,int> mxid, mnid;
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];
mxid = {i, j};
}
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
if(g[i][j] < mn) {
mn = g[i][j];
mnid = {i, j};
}else if(g[i][j] == mn and dis({i, j}, mxid) < dis(mnid, mxid)) {
mn = g[i][j];
mnid = {i, j};
}
}
}
cout << mx - mn - (dis(mxid, mnid)+1);
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... |