結果

問題 No.5020 Averaging
ユーザー KlavisKlavis
提出日時 2024-02-25 13:53:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 70 ms / 1,000 ms
コード長 1,610 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 63,748 KB
スコア 14,365,625
最終ジャッジ日時 2024-02-25 13:53:46
合計ジャッジ時間 4,504 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
56,404 KB
testcase_01 AC 42 ms
56,404 KB
testcase_02 AC 42 ms
56,404 KB
testcase_03 AC 41 ms
56,404 KB
testcase_04 AC 41 ms
56,404 KB
testcase_05 AC 45 ms
56,404 KB
testcase_06 AC 42 ms
56,404 KB
testcase_07 AC 43 ms
56,404 KB
testcase_08 AC 41 ms
56,404 KB
testcase_09 AC 42 ms
56,404 KB
testcase_10 AC 42 ms
56,404 KB
testcase_11 AC 70 ms
56,404 KB
testcase_12 AC 42 ms
56,404 KB
testcase_13 AC 53 ms
63,748 KB
testcase_14 AC 43 ms
56,404 KB
testcase_15 AC 41 ms
56,404 KB
testcase_16 AC 41 ms
56,404 KB
testcase_17 AC 41 ms
56,404 KB
testcase_18 AC 43 ms
56,404 KB
testcase_19 AC 42 ms
56,404 KB
testcase_20 AC 42 ms
56,404 KB
testcase_21 AC 42 ms
56,404 KB
testcase_22 AC 43 ms
56,404 KB
testcase_23 AC 42 ms
56,404 KB
testcase_24 AC 42 ms
56,404 KB
testcase_25 AC 41 ms
56,404 KB
testcase_26 AC 41 ms
56,404 KB
testcase_27 AC 41 ms
56,404 KB
testcase_28 AC 43 ms
56,404 KB
testcase_29 AC 44 ms
56,404 KB
testcase_30 AC 45 ms
56,404 KB
testcase_31 AC 42 ms
56,404 KB
testcase_32 AC 52 ms
63,748 KB
testcase_33 AC 41 ms
56,404 KB
testcase_34 AC 44 ms
56,404 KB
testcase_35 AC 42 ms
56,404 KB
testcase_36 AC 42 ms
56,404 KB
testcase_37 AC 41 ms
56,404 KB
testcase_38 AC 42 ms
56,404 KB
testcase_39 AC 41 ms
56,404 KB
testcase_40 AC 42 ms
56,404 KB
testcase_41 AC 41 ms
56,404 KB
testcase_42 AC 43 ms
56,404 KB
testcase_43 AC 41 ms
56,404 KB
testcase_44 AC 41 ms
56,404 KB
testcase_45 AC 41 ms
56,404 KB
testcase_46 AC 41 ms
56,404 KB
testcase_47 AC 42 ms
56,404 KB
testcase_48 AC 44 ms
56,404 KB
testcase_49 AC 41 ms
56,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random
import math

N = int(input())  # カードの枚数
A = [0] * N  # 各カードの表面の数値
B = [0] * N  # 各カードの裏面の数値

for i in range(N):
    A[i], B[i] = map(int, input().split())

# 目標値
target = 5 * 10**17

# 初期温度、終了温度、冷却率
T0 = 1000
Tf = 1
alpha = 0.99

def evaluate(A, B, target):
    """評価関数:カード1の表裏の数値と目標値との差の絶対値の和"""
    return abs(A[0] - target) + abs(B[0] - target)

def simulated_annealing(A, B, T0, Tf, alpha, max_operations=50):
    operations = []
    T = T0
    current_score = evaluate(A, B, target)
    while T > Tf and len(operations) < max_operations:
        # ランダムに2枚のカードを選択
        u, v = random.sample(range(N), 2)
        # 操作を試みる
        new_A_u, new_A_v = (A[u] + A[v]) // 2, (A[u] + A[v]) // 2
        new_B_u, new_B_v = (B[u] + B[v]) // 2, (B[u] + B[v]) // 2
        # 一時的に更新
        A[u], A[v], B[u], B[v] = new_A_u, new_A_v, new_B_u, new_B_v
        new_score = evaluate(A, B, target)
        delta = new_score - current_score
        if delta < 0 or random.random() < math.exp(-delta / T):
            # 更新を確定
            current_score = new_score
            operations.append((u+1, v+1))
        else:
            # 更新を取り消し
            A[u], A[v], B[u], B[v] = A[u], A[v], B[u], B[v]
        # 温度の更新
        T *= alpha
    return operations

operations = simulated_annealing(A, B, T0, Tf, alpha)

# 出力
print(len(operations))
for u, v in operations:
    print(u, v)
0