結果
問題 | No.2157 崖 |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:43:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,469 ms / 6,000 ms |
コード長 | 2,157 bytes |
コンパイル時間 | 186 ms |
コンパイル使用メモリ | 82,744 KB |
実行使用メモリ | 251,488 KB |
最終ジャッジ日時 | 2025-03-31 17:44:10 |
合計ジャッジ時間 | 15,313 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 22 |
ソースコード
import bisectdef main():import sysinput = sys.stdin.read().split()idx = 0N, M = int(input[idx]), int(input[idx+1])idx +=2D = []for _ in range(N):row = list(map(int, input[idx:idx+M]))idx += MD.append(row)# Initialize DP for the first rowcurrent = sorted(D[0])unique_current = []prev_x = Nonefor x in current:if x != prev_x:unique_current.append(x)prev_x = xdp_prev = {x: 0 for x in unique_current}if not dp_prev:print(-1)returnk = 5 # Number of neighboring elements to checkfor i in range(1, N):current_row = D[i]current_row_sorted = sorted(current_row)# Create unique x candidates in sorted orderunique_x = []prev_x = Nonefor x in current_row_sorted:if x != prev_x:unique_x.append(x)prev_x = xdp_current = {}prev_ys = sorted(dp_prev.keys())if not prev_ys:print(-1)returnfor x in sorted(unique_x):j = bisect.bisect_right(prev_ys, x) - 1if j < 0:continue # no valid ystart = max(0, j - k)end = min(len(prev_ys)-1, j + k)min_max = float('inf')for yj in range(start, end + 1):y = prev_ys[yj]if y > x:continuecurrent_max = max(dp_prev[y], x - y)if current_max < min_max:min_max = current_maxif min_max != float('inf'):if x in dp_current:if min_max < dp_current[x]:dp_current[x] = min_maxelse:dp_current[x] = min_maxdp_prev = dp_currentif not dp_prev:print(-1)returnif dp_prev:print(min(dp_prev.values()))else:print(-1)if __name__ == '__main__':main()