結果
問題 | No.1077 Noelちゃんと星々4 |
ユーザー |
![]() |
提出日時 | 2025-03-20 20:45:18 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,736 bytes |
コンパイル時間 | 147 ms |
コンパイル使用メモリ | 82,548 KB |
実行使用メモリ | 77,492 KB |
最終ジャッジ日時 | 2025-03-20 20:45:28 |
合計ジャッジ時間 | 2,106 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 1 WA * 19 |
ソースコード
import bisectn = int(input())Y = list(map(int, input().split()))if n == 0:print(0)exit()current_candidates = {}start = max(0, Y[0] - 1000)end = Y[0] + 1000for v in range(start, end + 1):cost = abs(Y[0] - v)current_candidates[v] = costprev_sorted = sorted(current_candidates.keys())for i in range(1, n):# Prepare min_prefix using prev_sortedmin_prefix = {}min_so_far = float('inf')for v in prev_sorted:if current_candidates[v] < min_so_far:min_so_far = current_candidates[v]min_prefix[v] = min_so_farnew_candidates = {}yi = Y[i]# Determine the search range for current v_iif prev_sorted:lower_bound = prev_sorted[0]else:break # should not happen as n >=1start_i = max(lower_bound, yi - 1000)end_i = yi + 1000# Iterate possible v_i in the range, using step 1 for integer v_ifor v_i in range(start_i, end_i + 1):# Find the largest v_prev <= v_i using binary search on prev_sortedidx = bisect.bisect_right(prev_sorted, v_i) - 1if idx < 0:continue # no valid v_prev <= v_ibest_v_prev = prev_sorted[idx]current_min = min_prefix[best_v_prev]cost = current_min + abs(yi - v_i)# Update new_candidatesif v_i not in new_candidates or cost < new_candidates[v_i]:new_candidates[v_i] = cost# Update for next iterationcurrent_candidates = new_candidatesif not current_candidates:break # no possible sequence, though problem states n>=1prev_sorted = sorted(current_candidates.keys())if current_candidates:print(min(current_candidates.values()))else:print(0)