import random import math from time import perf_counter import sys class TimeKeeper: def __init__(self): self.start_time = perf_counter() def is_time_over(self, LIMIT): return (perf_counter() - self.start_time) >= LIMIT def time_now(self): return (perf_counter() - self.start_time) # 定数とグローバル変数 TARGET = 500000000000000000 N = 0 A = [] B = [] Order = [] BestOrder = [] def get_score(): V1, V2 = A[0], B[0] for i in range(N - 1): V1 = (V1 + A[Order[i]]) // 2 V2 = (V2 + B[Order[i]]) // 2 return max(abs(TARGET - V1), abs(TARGET - V2)) def main(): global N, A, B, Order, BestOrder tk = TimeKeeper() LIMIT = 0.85 N = int(input()) A = [0] * N B = [0] * N for i in range(N): A[i], B[i] = map(int, input().split()) # 初期化 Order = list(range(1, N)) current_score = get_score() best_score = current_score # 焼きなまし loop = 0 T0, T1 = 0.2, 0.2 T = T0 while True: loop += 1 if loop % 1000 == 0: T = T0 + (T1 - T0) * tk.time_now() / LIMIT if tk.is_time_over(LIMIT): break idx1 = random.randrange(N - 1) idx2 = random.randrange(N - 1) Order[idx1], Order[idx2] = Order[idx2], Order[idx1] cand_score = get_score() # 変更候補 diff = math.log10(current_score) - math.log10(cand_score) # diff > 0:改善 if random.random() < math.exp(diff / T): # 採用 if best_score > cand_score: best_score = cand_score BestOrder = Order[:] current_score = cand_score else: # 不採用の場合は元に戻す Order[idx1], Order[idx2] = Order[idx2], Order[idx1] ANS = [(1, BestOrder[i]+1) for i in range(N - 1)] print(len(ANS)) for a in ANS: print(a[0], a[1]) sc = int(2*10**6 - 10**5 * math.log10(best_score + 1)) print(sc, best_score, loop, file=sys.stderr) if __name__ == "__main__": main()