#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2000 + 1;
int n, m, minn = 2e9, maxx = 0;
int mat[MAX_N][MAX_N], lin[MAX_N];
bool check( int x ) {
int last;
bool sePoate;
sePoate = true;
last = n;
for ( int c = 0; c < m; c++ ) {
int l = 0;
while ( l < last && mat[l][c] - minn <= x )
l++;
last = l;
for ( ; l < n; l++ ) {
if ( maxx - mat[l][c] > x )
sePoate = false;
}
}
if ( sePoate )
return true;
sePoate = true;
last = n;
for ( int c = m - 1; c >= 0; c-- ) {
int l = 0;
while ( l < last && mat[l][c] - minn <= x )
l++;
last = l;
for ( ; l < n; l++ ) {
if ( maxx - mat[l][c] > x )
sePoate = false;
}
}
if ( sePoate )
return true;
last = n;
sePoate = true;
for ( int c = 0; c < m; c++ ) {
int l = 0;
while ( l < last && maxx - mat[l][c] <= x )
l++;
last = l;
for ( ; l < n; l++ ) {
if ( mat[l][c] - minn > x )
sePoate = false;
}
}
if ( sePoate )
return true;
last = n;
sePoate = true;
for ( int c = m - 1; c >= 0; c-- ) {
int l = 0;
while ( l < last && maxx - mat[l][c] <= x )
l++;
last = l;
for ( ; l < n; l++ ) {
if ( mat[l][c] - minn > x )
sePoate = false;
}
}
if ( sePoate )
return true;
return false;
}
int main() {
cin.tie( NULL );
ios_base::sync_with_stdio( false );
cin >> n >> m;
for ( int l = 0; l < n; l++ ) {
for ( int c = 0; c < m; c++ ) {
cin >> mat[l][c];
minn = min( minn, mat[l][c] );
maxx = max( maxx, mat[l][c] );
}
}
int l = -1, r = maxx - minn;
while ( r - l > 1 ) {
int mid = (l + r) / 2;
if ( check( mid ) )
r = mid;
else
l = mid;
}
cout << r;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
7 ms |
1492 KB |
Output is correct |
18 |
Correct |
6 ms |
1492 KB |
Output is correct |
19 |
Correct |
7 ms |
1496 KB |
Output is correct |
20 |
Correct |
6 ms |
1368 KB |
Output is correct |
21 |
Correct |
9 ms |
1620 KB |
Output is correct |
22 |
Correct |
11 ms |
1620 KB |
Output is correct |
23 |
Correct |
9 ms |
1612 KB |
Output is correct |
24 |
Correct |
7 ms |
1492 KB |
Output is correct |
25 |
Correct |
7 ms |
1608 KB |
Output is correct |
26 |
Correct |
9 ms |
1608 KB |
Output is correct |
27 |
Correct |
9 ms |
1624 KB |
Output is correct |
28 |
Correct |
10 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
7 ms |
1492 KB |
Output is correct |
18 |
Correct |
6 ms |
1492 KB |
Output is correct |
19 |
Correct |
7 ms |
1496 KB |
Output is correct |
20 |
Correct |
6 ms |
1368 KB |
Output is correct |
21 |
Correct |
9 ms |
1620 KB |
Output is correct |
22 |
Correct |
11 ms |
1620 KB |
Output is correct |
23 |
Correct |
9 ms |
1612 KB |
Output is correct |
24 |
Correct |
7 ms |
1492 KB |
Output is correct |
25 |
Correct |
7 ms |
1608 KB |
Output is correct |
26 |
Correct |
9 ms |
1608 KB |
Output is correct |
27 |
Correct |
9 ms |
1624 KB |
Output is correct |
28 |
Correct |
10 ms |
1620 KB |
Output is correct |
29 |
Correct |
1538 ms |
15216 KB |
Output is correct |
30 |
Correct |
1938 ms |
16004 KB |
Output is correct |
31 |
Correct |
2448 ms |
16072 KB |
Output is correct |
32 |
Correct |
2056 ms |
16000 KB |
Output is correct |
33 |
Correct |
1448 ms |
14080 KB |
Output is correct |
34 |
Correct |
2055 ms |
16000 KB |
Output is correct |
35 |
Execution timed out |
4077 ms |
16012 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |