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 tuple<ll,ll,ll> trio;
typedef pair<int,int> pii;
const int MAXN = 2e5+5;
const ll INF = 1e9;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mk make_pair
#define pb push_back
#define fr first
#define sc second
int n, m, max_val, min_val = 1e9, op[4] = {0, 0, 1, -1};
int color[2005][2005], v[2005][2005];
bool vis[2005][2005];
void dfs(int x, int y, int flag) {
vis[x][y] = 1;
for(int i = 0; i < 4; i++) {
int a = x + op[i], b = y + op[3-i];
if(a <= 0 or n < a or b <= 0 or m < b) continue;
if(!vis[a][b] and v[a][b] != -flag) dfs(a, b, flag);
}
}
bool check(int dif) {
int a, b, c, d;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
color[i][j] = 0;
if(max_val - dif >= v[i][j]) color[i][j] = 1, a = i, b = j;
if(min_val + dif <= v[i][j]) color[i][j] = -1, c = i, d = j;
if(a == c and b == d) return 0;
}
}
// checa se os menores chegam um no outro
memset(vis, 0, sizeof(vis));
dfs(a,b,1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(color[i][j] == 1 and !vis[i][j]) return 0;
// checa se os maiores chegam um no outro
memset(vis, 0, sizeof(vis));
dfs(c,d,-1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(color[i][j] == -1 and !vis[i][j]) return 0;
int last = 0, cnt = 0;
for(int j = 1; j <= m; j++)
if(color[1][j]) cnt += (last != color[1][j]), last = color[1][j];
for(int i = 1; i <= n; i++)
if(color[i][m]) cnt += (last != color[i][m]), last = color[i][m];
for(int j = m; j >= 1; j--)
if(color[n][j]) cnt += (last != color[n][j]), last = color[n][j];
for(int i = n; i >= 1; i--)
if(color[i][1]) cnt += (last != color[i][1]), last = color[i][1];
if(cnt > 3) return 0;
return 1;
}
int busca() {
int l = 1, r = max_val - min_val;
while(l <= r) {
int mid = (l+r)/2;
if(check(mid)) r = mid-1;
else l = mid+1;
}
return r;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> v[i][j];
min_val = min(min_val, v[i][j]);
max_val = max(max_val, v[i][j]);
}
}
cout << busca();
}
Compilation message (stderr)
joioi.cpp: In function 'bool check(int)':
joioi.cpp:53:8: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
53 | dfs(c,d,-1);
| ~~~^~~~~~~~
joioi.cpp:53:8: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
joioi.cpp:45:8: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
45 | dfs(a,b,1);
| ~~~^~~~~~~
joioi.cpp:45:8: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |