#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+1;
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+1;
break;
}
}
for (int j=pos; j<m; j++) {
cur = min(cur, grid[i][j]);
}
}
if (max_v-cur <= x) return true;
pos = m;
cur = min_v;
for (int i=0; i<n; i++) {
for (int j=0; j<pos; j++) {
if (max_v-grid[i][j] > x) {
pos = j+1;
break;
}
}
for (int j=pos; j<m; j++) {
cur = max(cur, grid[i][j]);
}
}
if (cur-min_v <= x) return true;
pos = m;
cur = min_v;
for (int i=n-1; i>=0; i--) {
for (int j=0; j<pos; j++) {
if (max_v-grid[i][j] > x) {
pos = j+1;
break;
}
}
for (int j=pos; j<m; j++) {
cur = max(cur, grid[i][j]);
}
}
if (cur-min_v <= 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;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |