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))
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
20008 KB |
Output is correct |
2 |
Correct |
58 ms |
19732 KB |
Output is correct |
3 |
Correct |
70 ms |
20228 KB |
Output is correct |
4 |
Correct |
80 ms |
20752 KB |
Output is correct |
5 |
Correct |
73 ms |
20496 KB |
Output is correct |
6 |
Correct |
93 ms |
20744 KB |
Output is correct |
7 |
Correct |
86 ms |
20504 KB |
Output is correct |
8 |
Correct |
114 ms |
20220 KB |
Output is correct |
9 |
Correct |
68 ms |
20408 KB |
Output is correct |
10 |
Correct |
74 ms |
20184 KB |
Output is correct |
11 |
Correct |
79 ms |
19988 KB |
Output is correct |
12 |
Correct |
69 ms |
20472 KB |
Output is correct |
13 |
Correct |
73 ms |
20604 KB |
Output is correct |
14 |
Correct |
80 ms |
19848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
20008 KB |
Output is correct |
2 |
Correct |
58 ms |
19732 KB |
Output is correct |
3 |
Correct |
70 ms |
20228 KB |
Output is correct |
4 |
Correct |
80 ms |
20752 KB |
Output is correct |
5 |
Correct |
73 ms |
20496 KB |
Output is correct |
6 |
Correct |
93 ms |
20744 KB |
Output is correct |
7 |
Correct |
86 ms |
20504 KB |
Output is correct |
8 |
Correct |
114 ms |
20220 KB |
Output is correct |
9 |
Correct |
68 ms |
20408 KB |
Output is correct |
10 |
Correct |
74 ms |
20184 KB |
Output is correct |
11 |
Correct |
79 ms |
19988 KB |
Output is correct |
12 |
Correct |
69 ms |
20472 KB |
Output is correct |
13 |
Correct |
73 ms |
20604 KB |
Output is correct |
14 |
Correct |
80 ms |
19848 KB |
Output is correct |
15 |
Correct |
162 ms |
26292 KB |
Output is correct |
16 |
Correct |
306 ms |
32856 KB |
Output is correct |
17 |
Correct |
366 ms |
35292 KB |
Output is correct |
18 |
Correct |
428 ms |
34936 KB |
Output is correct |
19 |
Correct |
453 ms |
34672 KB |
Output is correct |
20 |
Correct |
350 ms |
34752 KB |
Output is correct |
21 |
Correct |
362 ms |
35312 KB |
Output is correct |
22 |
Correct |
374 ms |
34816 KB |
Output is correct |
23 |
Correct |
414 ms |
34884 KB |
Output is correct |
24 |
Correct |
343 ms |
34648 KB |
Output is correct |
25 |
Correct |
347 ms |
34600 KB |
Output is correct |
26 |
Correct |
385 ms |
34540 KB |
Output is correct |
27 |
Correct |
379 ms |
34060 KB |
Output is correct |
28 |
Correct |
348 ms |
34656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
20008 KB |
Output is correct |
2 |
Correct |
58 ms |
19732 KB |
Output is correct |
3 |
Correct |
70 ms |
20228 KB |
Output is correct |
4 |
Correct |
80 ms |
20752 KB |
Output is correct |
5 |
Correct |
73 ms |
20496 KB |
Output is correct |
6 |
Correct |
93 ms |
20744 KB |
Output is correct |
7 |
Correct |
86 ms |
20504 KB |
Output is correct |
8 |
Correct |
114 ms |
20220 KB |
Output is correct |
9 |
Correct |
68 ms |
20408 KB |
Output is correct |
10 |
Correct |
74 ms |
20184 KB |
Output is correct |
11 |
Correct |
79 ms |
19988 KB |
Output is correct |
12 |
Correct |
69 ms |
20472 KB |
Output is correct |
13 |
Correct |
73 ms |
20604 KB |
Output is correct |
14 |
Correct |
80 ms |
19848 KB |
Output is correct |
15 |
Correct |
162 ms |
26292 KB |
Output is correct |
16 |
Correct |
306 ms |
32856 KB |
Output is correct |
17 |
Correct |
366 ms |
35292 KB |
Output is correct |
18 |
Correct |
428 ms |
34936 KB |
Output is correct |
19 |
Correct |
453 ms |
34672 KB |
Output is correct |
20 |
Correct |
350 ms |
34752 KB |
Output is correct |
21 |
Correct |
362 ms |
35312 KB |
Output is correct |
22 |
Correct |
374 ms |
34816 KB |
Output is correct |
23 |
Correct |
414 ms |
34884 KB |
Output is correct |
24 |
Correct |
343 ms |
34648 KB |
Output is correct |
25 |
Correct |
347 ms |
34600 KB |
Output is correct |
26 |
Correct |
385 ms |
34540 KB |
Output is correct |
27 |
Correct |
379 ms |
34060 KB |
Output is correct |
28 |
Correct |
348 ms |
34656 KB |
Output is correct |
29 |
Runtime error |
1286 ms |
262144 KB |
Execution killed with signal 9 |
30 |
Halted |
0 ms |
0 KB |
- |