#include <iostream>
using namespace std;
#define MAXN 2005
int n, m;
int grid[MAXN][MAXN];
int min_v = 1e9, max_v = -1;
bool check (int x) {
int pos = m;
int cur = max_v;
for (int i=0; i<n; i++) {
for (int j=0; j<pos; j++) {
if (grid[i][j]-min_v > x) {
pos = j;
break;
}
}
for (int j=pos; j<m; j++) {
cur = min(cur, grid[i][j]);
}
}
if (max_v-cur <= x) return true;
pos = m;
cur = max_v;
for (int i=n-1; i>=0; i--) {
for (int j=0; j<pos; j++) {
if (grid[i][j]-min_v > x) {
pos = j;
break;
}
}
for (int j=pos; j<m; j++) {
cur = min(cur, grid[i][j]);
}
}
if (max_v-cur <= x) return true;
pos = -1;
cur = max_v;
for (int i=0; i<n; i++) {
for (int j=m-1; j>pos; j--) {
if (grid[i][j]-min_v > x) {
pos = j;
break;
}
}
for (int j=pos; j>=0; j--) {
cur = min(cur, grid[i][j]);
}
}
if (max_v-cur <= x) return true;
pos = -1;
cur = max_v;
for (int i=n-1; i>=0; i--) {
for (int j=m-1; j>pos; j--) {
if (grid[i][j]-min_v > x) {
pos = j;
break;
}
}
for (int j=pos; j>=0; j--) {
cur = min(cur, grid[i][j]);
}
}
if (max_v-cur <= x) return true;
return false;
}
int main () {
cin >> n >> m;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin >> grid[i][j];
min_v = min(min_v, grid[i][j]);
max_v = max(max_v, grid[i][j]);
}
}
int l = 0, r = 1e9, m, o;
while (l <= r) {
m = (l+r)/2;
if (check(m)) {
r = m-1; o = m;
} else {
l = m+1;
}
}
cout << o << endl;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:84:10: warning: 'o' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | cout << o << endl;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
12 ms |
1280 KB |
Output is correct |
17 |
Correct |
28 ms |
1536 KB |
Output is correct |
18 |
Correct |
29 ms |
1536 KB |
Output is correct |
19 |
Correct |
30 ms |
1528 KB |
Output is correct |
20 |
Correct |
28 ms |
1408 KB |
Output is correct |
21 |
Correct |
39 ms |
1660 KB |
Output is correct |
22 |
Correct |
40 ms |
1656 KB |
Output is correct |
23 |
Correct |
39 ms |
1632 KB |
Output is correct |
24 |
Correct |
34 ms |
1512 KB |
Output is correct |
25 |
Correct |
41 ms |
1604 KB |
Output is correct |
26 |
Correct |
43 ms |
1660 KB |
Output is correct |
27 |
Correct |
44 ms |
1720 KB |
Output is correct |
28 |
Correct |
39 ms |
1656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
12 ms |
1280 KB |
Output is correct |
17 |
Correct |
28 ms |
1536 KB |
Output is correct |
18 |
Correct |
29 ms |
1536 KB |
Output is correct |
19 |
Correct |
30 ms |
1528 KB |
Output is correct |
20 |
Correct |
28 ms |
1408 KB |
Output is correct |
21 |
Correct |
39 ms |
1660 KB |
Output is correct |
22 |
Correct |
40 ms |
1656 KB |
Output is correct |
23 |
Correct |
39 ms |
1632 KB |
Output is correct |
24 |
Correct |
34 ms |
1512 KB |
Output is correct |
25 |
Correct |
41 ms |
1604 KB |
Output is correct |
26 |
Correct |
43 ms |
1660 KB |
Output is correct |
27 |
Correct |
44 ms |
1720 KB |
Output is correct |
28 |
Correct |
39 ms |
1656 KB |
Output is correct |
29 |
Correct |
2412 ms |
37468 KB |
Output is correct |
30 |
Correct |
2454 ms |
37624 KB |
Output is correct |
31 |
Correct |
2494 ms |
39332 KB |
Output is correct |
32 |
Correct |
2551 ms |
39476 KB |
Output is correct |
33 |
Correct |
2177 ms |
34320 KB |
Output is correct |
34 |
Correct |
2612 ms |
39480 KB |
Output is correct |
35 |
Correct |
3768 ms |
55116 KB |
Output is correct |
36 |
Correct |
3359 ms |
49636 KB |
Output is correct |
37 |
Execution timed out |
4022 ms |
54988 KB |
Time limit exceeded |