#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, v[2005][2005], color[2005][2005];
bool check(int dif) {
for(int i = 1; i <= n; i++) {
for(int j = 1, a, b, c, d; 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;
}
}
bool c_pos = 1, d_pos = 1, c_neg = 1, d_neg = 1;
for(int i = 1, last_c = 0, last_d = m; i <= n; i++) {
int At = 0, Max = m;
for(int j = 1; j <= m; j++) {
if(color[i][j] == 0) continue;
if(color[i][j] == 1) At = j;
else if(j <= Max) Max = j-1;
}
last_c = max(last_c, At);
if(last_c > Max) c_pos = 0;
last_d = min(last_d, Max);
if(last_d < At) d_pos = 0;
}
for(int i = 1, last_c = 0, last_d = m; i <= n; i++) {
int At = 0, Max = m;
for(int j = 1; j <= m; j++) {
if(color[i][j] == 0) continue;
if(color[i][j] == -1) At = j;
else if(j <= Max) Max = j-1;
}
last_c = max(last_c, At);
if(last_c > Max) c_neg = 0;
last_d = min(last_d, Max);
if(last_d < At) d_neg = 0;
}
return c_pos or d_pos or c_neg or d_neg;
}
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
joioi.cpp: In function 'bool check(int)':
joioi.cpp:26:29: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | if(a == c and b == d) return 0;
| ~~^~~~
joioi.cpp:26:18: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | if(a == c and b == d) return 0;
| ~~^~~~
joioi.cpp:26:29: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | if(a == c and b == d) return 0;
| ~~^~~~
joioi.cpp:26:18: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | if(a == c and b == d) return 0;
| ~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2564 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2564 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2436 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
5 ms |
6736 KB |
Output is correct |
17 |
Correct |
7 ms |
6992 KB |
Output is correct |
18 |
Correct |
5 ms |
6992 KB |
Output is correct |
19 |
Correct |
12 ms |
6888 KB |
Output is correct |
20 |
Correct |
11 ms |
6736 KB |
Output is correct |
21 |
Correct |
9 ms |
6992 KB |
Output is correct |
22 |
Correct |
6 ms |
6992 KB |
Output is correct |
23 |
Correct |
13 ms |
6992 KB |
Output is correct |
24 |
Correct |
6 ms |
6988 KB |
Output is correct |
25 |
Correct |
20 ms |
7160 KB |
Output is correct |
26 |
Correct |
9 ms |
6992 KB |
Output is correct |
27 |
Correct |
9 ms |
7008 KB |
Output is correct |
28 |
Correct |
9 ms |
6992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2564 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2436 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
5 ms |
6736 KB |
Output is correct |
17 |
Correct |
7 ms |
6992 KB |
Output is correct |
18 |
Correct |
5 ms |
6992 KB |
Output is correct |
19 |
Correct |
12 ms |
6888 KB |
Output is correct |
20 |
Correct |
11 ms |
6736 KB |
Output is correct |
21 |
Correct |
9 ms |
6992 KB |
Output is correct |
22 |
Correct |
6 ms |
6992 KB |
Output is correct |
23 |
Correct |
13 ms |
6992 KB |
Output is correct |
24 |
Correct |
6 ms |
6988 KB |
Output is correct |
25 |
Correct |
20 ms |
7160 KB |
Output is correct |
26 |
Correct |
9 ms |
6992 KB |
Output is correct |
27 |
Correct |
9 ms |
7008 KB |
Output is correct |
28 |
Correct |
9 ms |
6992 KB |
Output is correct |
29 |
Correct |
623 ms |
53320 KB |
Output is correct |
30 |
Correct |
740 ms |
53324 KB |
Output is correct |
31 |
Correct |
1138 ms |
54856 KB |
Output is correct |
32 |
Correct |
560 ms |
54932 KB |
Output is correct |
33 |
Correct |
240 ms |
51668 KB |
Output is correct |
34 |
Correct |
865 ms |
55044 KB |
Output is correct |
35 |
Correct |
756 ms |
70544 KB |
Output is correct |
36 |
Correct |
403 ms |
65352 KB |
Output is correct |
37 |
Correct |
1472 ms |
70612 KB |
Output is correct |