#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 () {
ios_base::sync_with_stdio(false);
cin.tie(0);
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:86:10: warning: 'o' may be used uninitialized in this function [-Wmaybe-uninitialized]
86 | cout << o << endl;
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
0 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 |
5 ms |
1408 KB |
Output is correct |
17 |
Correct |
8 ms |
1536 KB |
Output is correct |
18 |
Correct |
8 ms |
1536 KB |
Output is correct |
19 |
Correct |
9 ms |
1536 KB |
Output is correct |
20 |
Correct |
8 ms |
1408 KB |
Output is correct |
21 |
Correct |
11 ms |
1664 KB |
Output is correct |
22 |
Correct |
13 ms |
1664 KB |
Output is correct |
23 |
Correct |
11 ms |
1664 KB |
Output is correct |
24 |
Correct |
9 ms |
1536 KB |
Output is correct |
25 |
Correct |
10 ms |
1664 KB |
Output is correct |
26 |
Correct |
10 ms |
1664 KB |
Output is correct |
27 |
Correct |
10 ms |
1664 KB |
Output is correct |
28 |
Correct |
10 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
0 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 |
5 ms |
1408 KB |
Output is correct |
17 |
Correct |
8 ms |
1536 KB |
Output is correct |
18 |
Correct |
8 ms |
1536 KB |
Output is correct |
19 |
Correct |
9 ms |
1536 KB |
Output is correct |
20 |
Correct |
8 ms |
1408 KB |
Output is correct |
21 |
Correct |
11 ms |
1664 KB |
Output is correct |
22 |
Correct |
13 ms |
1664 KB |
Output is correct |
23 |
Correct |
11 ms |
1664 KB |
Output is correct |
24 |
Correct |
9 ms |
1536 KB |
Output is correct |
25 |
Correct |
10 ms |
1664 KB |
Output is correct |
26 |
Correct |
10 ms |
1664 KB |
Output is correct |
27 |
Correct |
10 ms |
1664 KB |
Output is correct |
28 |
Correct |
10 ms |
1664 KB |
Output is correct |
29 |
Correct |
648 ms |
15484 KB |
Output is correct |
30 |
Correct |
678 ms |
16108 KB |
Output is correct |
31 |
Correct |
642 ms |
16376 KB |
Output is correct |
32 |
Correct |
744 ms |
16376 KB |
Output is correct |
33 |
Correct |
634 ms |
14564 KB |
Output is correct |
34 |
Correct |
752 ms |
16512 KB |
Output is correct |
35 |
Correct |
886 ms |
16840 KB |
Output is correct |
36 |
Correct |
838 ms |
16660 KB |
Output is correct |
37 |
Correct |
1008 ms |
16656 KB |
Output is correct |