結果

問題 No.5020 Averaging
ユーザー brthyyjpbrthyyjp
提出日時 2024-02-25 14:56:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 890 ms / 1,000 ms
コード長 3,013 bytes
コンパイル時間 228 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 86,520 KB
スコア 36,546,022
最終ジャッジ日時 2024-02-25 14:57:02
合計ジャッジ時間 47,259 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 886 ms
84,460 KB
testcase_01 AC 887 ms
84,652 KB
testcase_02 AC 887 ms
85,656 KB
testcase_03 AC 885 ms
85,520 KB
testcase_04 AC 887 ms
83,920 KB
testcase_05 AC 887 ms
85,348 KB
testcase_06 AC 886 ms
84,304 KB
testcase_07 AC 888 ms
84,368 KB
testcase_08 AC 887 ms
85,248 KB
testcase_09 AC 886 ms
84,584 KB
testcase_10 AC 887 ms
84,540 KB
testcase_11 AC 888 ms
86,520 KB
testcase_12 AC 889 ms
84,976 KB
testcase_13 AC 888 ms
84,916 KB
testcase_14 AC 886 ms
84,468 KB
testcase_15 AC 888 ms
85,452 KB
testcase_16 AC 887 ms
84,748 KB
testcase_17 AC 887 ms
84,456 KB
testcase_18 AC 887 ms
85,152 KB
testcase_19 AC 887 ms
84,124 KB
testcase_20 AC 887 ms
84,880 KB
testcase_21 AC 887 ms
84,856 KB
testcase_22 AC 888 ms
84,872 KB
testcase_23 AC 887 ms
84,828 KB
testcase_24 AC 886 ms
85,280 KB
testcase_25 AC 887 ms
81,004 KB
testcase_26 AC 887 ms
84,760 KB
testcase_27 AC 887 ms
84,248 KB
testcase_28 AC 887 ms
85,344 KB
testcase_29 AC 887 ms
84,460 KB
testcase_30 AC 890 ms
84,728 KB
testcase_31 AC 886 ms
84,988 KB
testcase_32 AC 887 ms
84,520 KB
testcase_33 AC 887 ms
84,332 KB
testcase_34 AC 887 ms
83,772 KB
testcase_35 AC 886 ms
84,976 KB
testcase_36 AC 887 ms
85,024 KB
testcase_37 AC 887 ms
85,084 KB
testcase_38 AC 890 ms
84,504 KB
testcase_39 AC 888 ms
84,228 KB
testcase_40 AC 888 ms
85,320 KB
testcase_41 AC 887 ms
85,228 KB
testcase_42 AC 890 ms
84,520 KB
testcase_43 AC 888 ms
85,412 KB
testcase_44 AC 887 ms
84,104 KB
testcase_45 AC 887 ms
84,412 KB
testcase_46 AC 886 ms
84,916 KB
testcase_47 AC 886 ms
84,468 KB
testcase_48 AC 887 ms
85,084 KB
testcase_49 AC 888 ms
84,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from time import time
start_time = time()

import sys
import io, os
input = sys.stdin.readline
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

import math
import random
random.seed(42)

m = 50
TIME_LIMIT = 0.85
L = 5*10**17

start_temp = 10**14
end_temp = 1000

def move_element(lst, i, j):
    lst_ = lst.copy()
    element = lst_.pop(i)
    lst_.insert(j, element)
    return lst_

n =  int(input())
A = [0]*n
B = [0]*n
for i in range(n):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

ans = []
for i in range(m-1):
    u, v = random.sample(range(n), 2)
    ans.append((u, v))
v = random.randint(1, n-1)
ans.append((0, v))

def calc_score(ans):
    Ac = A.copy()
    Bc = B.copy()
    for u, v in ans:
        x = (Ac[u]+Ac[v])//2
        Ac[u], Ac[v] = x, x
        y = (Bc[u]+Bc[v])//2
        Bc[u], Bc[v] = y, y
    v1 = abs(L-Ac[0])
    v2 = abs(L-Bc[0])
    cost = max(v1, v2)
    return cost

cost = calc_score(ans)
best_cost = cost
best_ans = ans.copy()
iter = 0
while True:
    iter += 1
    now_time = time()
    if now_time - start_time > TIME_LIMIT:
        break
    neigh = neigh = random.randint(0, 1)
    if neigh == 0:
        # ランダムに1操作を選んで変更する
        i = random.randint(0, m-1)
        if i != m-1:
            idx = random.randint(0, 1)
            u = ans[i][idx]
            while True:
                v = random.randint(0, n-1)
                if v == u:
                    continue
                break
        else:
            v = random.randint(1, n-1)
            u = 0
        pre = ans[i]
        ans[i] = (u, v)
        new_cost = calc_score(ans)
        if new_cost <= best_cost:
            best_cost = new_cost
            best_ans = ans.copy()
        temp = start_temp+(end_temp-start_temp)*(now_time-start_time)/TIME_LIMIT
        if new_cost <= cost:
            prob = 1
        else:
            #prob = math.exp((cost-new_cost)/temp)
            prob = 0
        if prob > random.random():
            cost = new_cost
        else:
            ans[i] = pre
    else:
        # ランダムに1操作の位置を変更する
        i = random.randint(0, m-2)
        j = random.randint(0, m-2)
        new_ans = move_element(ans, i, j)
        new_cost = calc_score(new_ans)
        if new_cost <= best_cost:
            best_cost = new_cost
            best_ans = ans.copy()
        temp = start_temp+(end_temp-start_temp)*(now_time-start_time)/TIME_LIMIT
        if new_cost <= cost:
            prob = 1
        else:
            #prob = math.exp((cost-new_cost)/temp)
            prob = 0
        if prob > random.random():
            cost = new_cost
            ans = new_ans.copy()

print(len(best_ans))
for i in range(len(best_ans)):
    u, v = best_ans[i]
    u, v = u+1, v+1
    print(u, v)

if cost != 0:
    score = (int)(2000000-100000*math.log10(best_cost+1))
else:
    score = 2000050-len(best_ans)
print(cost, file=sys.stderr)
print(iter, file=sys.stderr)
print(score, file=sys.stderr)



0