結果

問題 No.5020 Averaging
ユーザー Ang1077Ang1077
提出日時 2024-02-25 15:49:35
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,223 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 102,004 KB
スコア 0
最終ジャッジ日時 2024-02-25 15:49:40
合計ジャッジ時間 4,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
from collections import deque, defaultdict
from itertools import (
    accumulate,
    product,
    permutations,
    combinations,
    combinations_with_replacement,
)
import math
from bisect import bisect_left, insort_left, bisect_right, insort_right
from pprint import pprint
from heapq import heapify, heappop, heappush
import string

# 小文字アルファベットのリスト
alph_s = list(string.ascii_lowercase)
# 大文字アルファベットのリスト
alph_l = list(string.ascii_uppercase)

# product : bit全探索 product(range(2),repeat=n)
# permutations : 順列全探索
# combinations : 組み合わせ(重複無し)
# combinations_with_replacement : 組み合わせ(重複可)
# from sortedcontainers import SortedSet, SortedList, SortedDict
sys.setrecursionlimit(10**7)
around4 = ((-1, 0), (1, 0), (0, -1), (0, 1))  # 上下左右
around8 = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
inf = float("inf")
mod = 998244353
input = lambda: sys.stdin.readline().rstrip()
P = lambda *x: print(*x)
PY = lambda: print("Yes")
PN = lambda: print("No")
II = lambda: int(input())
MII = lambda: map(int, input().split())
LMII = lambda: list(map(int, input().split()))


def dlist(*l, fill=0):
    if len(l) == 1:
        return [fill] * l[0]
    ll = l[1:]
    return [dlist(*ll, fill=fill) for _ in range(l[0])]


# 入力
def Input():
    global N, A, B, St, A_list, B_list
    St = 5 * 10**17
    N = II()
    A = [0] * N
    B = [0] * N
    for i in range(N):
        A[i], B[i] = MII()
    A_list = A[:]
    B_list = B[:]

    A = tuple(A)
    B = tuple(B)


def solve():
    ans = beam()

    A_n, B_n = get_A_B(ans)
    tmp, _ = get_ans(A_n, B_n, ans)
    ans = [(i + 1, j + 1) for i, j in ans]
    for i in tmp:
        ans.append(i)

    A_n = A_list[:]
    B_n = B_list[:]

    # for i, j in ans:
    #     A_n[i - 1], A_n[j - 1] = (A_n[i - 1] + A_n[j - 1]) // 2, (
    #         A_n[i - 1] + A_n[j - 1]
    #     ) // 2
    #     B_n[i - 1], B_n[j - 1] = (B_n[i - 1] + B_n[j - 1]) // 2, (
    #         B_n[i - 1] + B_n[j - 1]
    #     ) // 2
    #     print(max(abs(St - A_n[0]) // 1000000, abs(St - B_n[0]) // 1000000))

    return ans


def get_A_B(ans):
    A_n = A_list[:]
    B_n = B_list[:]
    # print(ans)
    for i, j in ans:
        A_n[i], A_n[j] = (A_n[i] + A_n[j]) // 2, (A_n[i] + A_n[j]) // 2
        B_n[i], B_n[j] = (B_n[i] + B_n[j]) // 2, (B_n[i] + B_n[j]) // 2
    return A_n, B_n


def get_ans(A, B, ans_prv):
    ans_list = []
    A_n = A[:]
    B_n = B[:]
    for _ in range(50 - len(ans_prv)):
        score = max(abs(St - A_n[0]), abs(St - B_n[0]))
        tmp = score
        ans = None
        for j in range(1, 45):
            a, b = (A_n[0] + A_n[j]) // 2, (B_n[0] + B_n[j]) // 2
            if max(abs(St - a), abs(St - b)) < tmp:
                tmp = max(abs(St - a), abs(St - b))
                ans = j
        if ans:
            ans_list.append([1, ans + 1])
            A_n[0], A_n[ans] = (A_n[0] + A_n[ans]) // 2, (A_n[0] + A_n[ans]) // 2
            B_n[0], B_n[ans] = (B_n[0] + B_n[ans]) // 2, (B_n[0] + B_n[ans]) // 2
        else:
            break
    return ans_list, max(abs(St - a), abs(St - b))


def beam():
    target_a, target_b = St - (A[0] - St), St - (B[0] - St)
    depth = 40
    width = 100
    heap = []
    heap.append((10**18, []))

    heap = deque(sorted(heap, key=lambda x: x[0])[:width])

    for idx in range(depth):
        new_heap = []
        while heap:
            score, ans = heap.pop()

            for i in range(0, 45):
                for j in range(i + 1, 45):
                    ans_n = ans[:]
                    ans_n.append((i, j))
                    A_n, B_n = get_A_B(ans_n)
                    _, tmp = get_ans(A_n, B_n, ans_n)

                    if tmp < score:
                        new_heap.append((tmp, ans_n))
        if len(new_heap) == 0:
            break

        heap = deque(sorted(new_heap, key=lambda x: x[0])[:width])
        # print(heap)
        _, ans = heap[0]

    return ans


# 出力
def Output(ans):
    print(len(ans))
    for i in ans:
        print(*i)


def main():
    Input()
    Output(solve())


if __name__ == "__main__":
    main()
0