from copy import *
n,m = map(int, input().split())
grid = [list(map(int, input().split()))for i in range(n)]
grid2 = [[0 for i in range(m)]for i in range(n)]
mx = -1
for i in range(n):
for j in range(m):
mx = max(mx, grid[i][j])
grid2[i][j] = grid[n-i-1][m-j-1]
minmaxl = [[[float("inf"), -1]for i in range(m)]for i in range(n)]
minmaxr = [[[float("inf"), -1]for i in range(m)]for i in range(n)]
def setup():
for i in range(n-1,-1,-1):
minmaxl[i][0] = [grid[i][0], grid[i][0]]
for j in range(1,m):
minmaxl[i][j][0] = min(minmaxl[i][j-1][0], grid[i][j])
minmaxl[i][j][1] = max(minmaxl[i][j-1][1], grid[i][j])
if i != n-1:
for j in range(m):
minmaxl[i][j][0] = min(minmaxl[i][j][0], minmaxl[i+1][j][0])
minmaxl[i][j][1] = max(minmaxl[i][j][1], minmaxl[i+1][j][1])
for i in range(n-1,-1,-1):
minmaxr[i][m-1] = [grid[i][m-1], grid[i][m-1]]
for j in range(m-2,-1,-1):
minmaxr[i][j][0] = min(minmaxr[i][j+1][0], grid[i][j])
minmaxr[i][j][1] = max(minmaxr[i][j+1][1], grid[i][j])
if i != n-1:
for j in range(m-1,-1,-1):
minmaxr[i][j][0] = min(minmaxr[i][j][0], minmaxr[i+1][j][0])
minmaxr[i][j][1] = max(minmaxr[i][j][1], minmaxr[i+1][j][1])
def check(a):
mini = float("inf")
maxi = -1
for i in range(n):
for j in range(m):
if mx - minmaxl[i][j][0] > a:
mini = min(mini, grid[i][j])
maxi = max(maxi, grid[i][j])
if maxi - mini <= a:
return True
mini = float("inf")
maxi = -1
for i in range(n):
for j in range(m):
if mx - minmaxr[i][j][0] > a:
mini = min(mini, grid[i][j])
maxi = max(maxi, grid[i][j])
if maxi - mini <= a:
return True
return False
setup()
lo = 0
hi = 10000000000
while (lo<hi):
mid = (lo+hi)//2
if check(mid):
hi = mid
else:
lo = mid+1
ans = lo
grid = deepcopy(grid2)
setup()
lo = 0
hi = 10000000000
while (lo<hi):
mid = (lo+hi)//2
if check(mid):
hi = mid
else:
lo = mid+1
print(min(ans,lo))
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3412 KB |
Output is correct |
2 |
Correct |
18 ms |
3280 KB |
Output is correct |
3 |
Correct |
25 ms |
3336 KB |
Output is correct |
4 |
Correct |
27 ms |
3336 KB |
Output is correct |
5 |
Correct |
30 ms |
3252 KB |
Output is correct |
6 |
Correct |
29 ms |
3284 KB |
Output is correct |
7 |
Correct |
25 ms |
3284 KB |
Output is correct |
8 |
Correct |
26 ms |
3284 KB |
Output is correct |
9 |
Correct |
22 ms |
3368 KB |
Output is correct |
10 |
Correct |
24 ms |
3360 KB |
Output is correct |
11 |
Correct |
25 ms |
3284 KB |
Output is correct |
12 |
Correct |
27 ms |
3356 KB |
Output is correct |
13 |
Correct |
33 ms |
3280 KB |
Output is correct |
14 |
Correct |
27 ms |
3384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3412 KB |
Output is correct |
2 |
Correct |
18 ms |
3280 KB |
Output is correct |
3 |
Correct |
25 ms |
3336 KB |
Output is correct |
4 |
Correct |
27 ms |
3336 KB |
Output is correct |
5 |
Correct |
30 ms |
3252 KB |
Output is correct |
6 |
Correct |
29 ms |
3284 KB |
Output is correct |
7 |
Correct |
25 ms |
3284 KB |
Output is correct |
8 |
Correct |
26 ms |
3284 KB |
Output is correct |
9 |
Correct |
22 ms |
3368 KB |
Output is correct |
10 |
Correct |
24 ms |
3360 KB |
Output is correct |
11 |
Correct |
25 ms |
3284 KB |
Output is correct |
12 |
Correct |
27 ms |
3356 KB |
Output is correct |
13 |
Correct |
33 ms |
3280 KB |
Output is correct |
14 |
Correct |
27 ms |
3384 KB |
Output is correct |
15 |
Correct |
91 ms |
3848 KB |
Output is correct |
16 |
Correct |
1113 ms |
13192 KB |
Output is correct |
17 |
Correct |
2090 ms |
15404 KB |
Output is correct |
18 |
Correct |
2412 ms |
15388 KB |
Output is correct |
19 |
Correct |
2445 ms |
15508 KB |
Output is correct |
20 |
Correct |
2205 ms |
13984 KB |
Output is correct |
21 |
Correct |
2356 ms |
15504 KB |
Output is correct |
22 |
Correct |
2973 ms |
15388 KB |
Output is correct |
23 |
Correct |
3027 ms |
15508 KB |
Output is correct |
24 |
Correct |
2792 ms |
14104 KB |
Output is correct |
25 |
Correct |
2816 ms |
15436 KB |
Output is correct |
26 |
Correct |
2561 ms |
15428 KB |
Output is correct |
27 |
Correct |
2519 ms |
15504 KB |
Output is correct |
28 |
Correct |
2227 ms |
15508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3412 KB |
Output is correct |
2 |
Correct |
18 ms |
3280 KB |
Output is correct |
3 |
Correct |
25 ms |
3336 KB |
Output is correct |
4 |
Correct |
27 ms |
3336 KB |
Output is correct |
5 |
Correct |
30 ms |
3252 KB |
Output is correct |
6 |
Correct |
29 ms |
3284 KB |
Output is correct |
7 |
Correct |
25 ms |
3284 KB |
Output is correct |
8 |
Correct |
26 ms |
3284 KB |
Output is correct |
9 |
Correct |
22 ms |
3368 KB |
Output is correct |
10 |
Correct |
24 ms |
3360 KB |
Output is correct |
11 |
Correct |
25 ms |
3284 KB |
Output is correct |
12 |
Correct |
27 ms |
3356 KB |
Output is correct |
13 |
Correct |
33 ms |
3280 KB |
Output is correct |
14 |
Correct |
27 ms |
3384 KB |
Output is correct |
15 |
Correct |
91 ms |
3848 KB |
Output is correct |
16 |
Correct |
1113 ms |
13192 KB |
Output is correct |
17 |
Correct |
2090 ms |
15404 KB |
Output is correct |
18 |
Correct |
2412 ms |
15388 KB |
Output is correct |
19 |
Correct |
2445 ms |
15508 KB |
Output is correct |
20 |
Correct |
2205 ms |
13984 KB |
Output is correct |
21 |
Correct |
2356 ms |
15504 KB |
Output is correct |
22 |
Correct |
2973 ms |
15388 KB |
Output is correct |
23 |
Correct |
3027 ms |
15508 KB |
Output is correct |
24 |
Correct |
2792 ms |
14104 KB |
Output is correct |
25 |
Correct |
2816 ms |
15436 KB |
Output is correct |
26 |
Correct |
2561 ms |
15428 KB |
Output is correct |
27 |
Correct |
2519 ms |
15504 KB |
Output is correct |
28 |
Correct |
2227 ms |
15508 KB |
Output is correct |
29 |
Execution timed out |
4067 ms |
216260 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |