結果

問題 No.5020 Averaging
ユーザー Mao-betaMao-beta
提出日時 2024-02-25 16:37:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 873 ms / 1,000 ms
コード長 7,124 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 76,696 KB
スコア 29,651,754
最終ジャッジ日時 2024-02-25 16:39:06
合計ジャッジ時間 41,257 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 807 ms
76,452 KB
testcase_01 AC 752 ms
76,448 KB
testcase_02 AC 764 ms
76,520 KB
testcase_03 AC 867 ms
76,472 KB
testcase_04 AC 669 ms
76,520 KB
testcase_05 AC 735 ms
76,428 KB
testcase_06 AC 760 ms
76,420 KB
testcase_07 AC 787 ms
76,548 KB
testcase_08 AC 761 ms
76,312 KB
testcase_09 AC 800 ms
76,448 KB
testcase_10 AC 782 ms
76,440 KB
testcase_11 AC 750 ms
76,420 KB
testcase_12 AC 752 ms
76,440 KB
testcase_13 AC 722 ms
76,396 KB
testcase_14 AC 704 ms
76,420 KB
testcase_15 AC 748 ms
76,420 KB
testcase_16 AC 751 ms
76,396 KB
testcase_17 AC 712 ms
76,308 KB
testcase_18 AC 807 ms
76,440 KB
testcase_19 AC 785 ms
76,420 KB
testcase_20 AC 803 ms
76,440 KB
testcase_21 AC 766 ms
76,396 KB
testcase_22 AC 702 ms
76,420 KB
testcase_23 AC 646 ms
76,520 KB
testcase_24 AC 707 ms
76,420 KB
testcase_25 AC 821 ms
76,696 KB
testcase_26 AC 793 ms
76,676 KB
testcase_27 AC 742 ms
76,392 KB
testcase_28 AC 814 ms
76,676 KB
testcase_29 AC 720 ms
76,396 KB
testcase_30 AC 864 ms
76,448 KB
testcase_31 AC 785 ms
76,444 KB
testcase_32 AC 773 ms
76,456 KB
testcase_33 AC 795 ms
76,396 KB
testcase_34 AC 873 ms
76,580 KB
testcase_35 AC 656 ms
76,420 KB
testcase_36 AC 684 ms
76,520 KB
testcase_37 AC 667 ms
76,392 KB
testcase_38 AC 830 ms
76,568 KB
testcase_39 AC 733 ms
76,420 KB
testcase_40 AC 786 ms
76,312 KB
testcase_41 AC 790 ms
76,312 KB
testcase_42 AC 766 ms
76,312 KB
testcase_43 AC 726 ms
76,420 KB
testcase_44 AC 765 ms
76,420 KB
testcase_45 AC 742 ms
76,396 KB
testcase_46 AC 724 ms
76,400 KB
testcase_47 AC 765 ms
76,448 KB
testcase_48 AC 718 ms
76,420 KB
testcase_49 AC 689 ms
76,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353

input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]


def score(a, b, x):
    v1 = abs(5 * 10**17 - a)
    v2 = abs(5 * 10**17 - b)
    if max(v1, v2) > 0:
        return int(2000000 - 100000 * math.log10(max(v1, v2)+1))
    else:
        return 2000050 - x


def answer(ans):
    print(len(ans))
    for i, j in ans:
        print(i+1, j+1)


def greedy():
    N = NI()
    AB = EI(N)

    ans = []
    # 0と何かだけで貪欲
    for qi in range(50):
        a0, b0 = AB[0]
        s = score(a0, b0, qi)
        best_s = s
        best_v = 1
        for v in range(1, N):
            av, bv = AB[v]
            na, nb = (a0+av) // 2, (b0+bv) // 2
            ns = score(na, nb, qi+1)

            if ns > best_s:
                best_s = ns
                best_v = v

        if best_s > s:
            av, bv = AB[best_v]
            na, nb = (a0 + av) // 2, (b0 + bv) // 2
            AB[0] = [na, nb]
            AB[best_v] = [na, nb]
            ans.append([0, best_v])
        else:
            break

    answer(ans)


def means(AB, i, j):
    ai, bi = AB[i]
    aj, bj = AB[j]
    na, nb = (ai + aj) // 2, (bi + bj) // 2
    return na, nb


def query(AB, i, j):
    na, nb = means(AB, i, j)
    AB[i] = [na, nb]
    AB[j] = [na, nb]


def total_score(AB):
    res = 0
    for a, b in AB:
        res += score(a, b, 0)
    return res


def calc_gap(AB, i, j):
    # ABについてi, jを操作したときのスコアの差分
    na, nb = means(AB, i, j)
    after = score(na, nb, 0) * 2
    before = score(AB[i][0], AB[i][1], 0) + score(AB[j][0], AB[j][1], 0)
    return after - before


def _main():
    N = NI()
    AB = EI(N)

    total = total_score(AB)
    # print(total)
    ans = []

    # totalが改善するように山登り
    for qi in range(50):

        max_gap = 0
        best_ij = [0, 1]
        for i in range(N):
            if qi == 49 and i > 0:
                break
            for j in range(i+1, N):
                gap = calc_gap(AB, i, j)
                if gap > max_gap:
                    max_gap = gap
                    best_ij = [i, j]
        if max_gap > 0:
            total += max_gap
            query(AB, *best_ij)
            ans.append(best_ij)
            # print(total, score(AB[0][0], AB[0][1], qi))
        else:
            break

    answer(ans)


def manhattan(a, b, ta, tb):
    v1 = abs(ta - a)
    v2 = abs(tb - b)
    return v1 + v2

def chebyshev(a, b, ta, tb):
    v1 = abs(ta - a)
    v2 = abs(tb - b)
    return max(v1, v2)

def twobytwo():
    N = NI()
    AB = EI(N)

    ans = []

    CENTER = 5 * 10**17

    # 二次元平面上で考える
    # 操作は「2点選んで中点につぶす」
    # AB[0]の反対側になるべく近いところになるように2点全探索
    # →そことAB[0]を選ぶ を繰り返す?

    prev_ij = [0, 1]

    for qi in range(50):
        if qi % 2 == 0:
            a0, b0 = AB[0]
            ta = CENTER * 2 - a0
            tb = CENTER * 2 - b0

            minm = CENTER * 2
            best_ij = [0, 1]
            for i in range(1, N):
                for j in range(i + 1, N):
                    na, nb = means(AB, i, j)
                    m = manhattan(na, nb, ta, tb)
                    if m < minm:
                        best_ij = [i, j]
                        minm = m

            if minm < CENTER * 2:
                query(AB, *best_ij)
                ans.append(best_ij)
                prev_ij = best_ij

        else:
            j = prev_ij[1]
            query(AB, 0, j)
            ans.append([0, j])

        # print(score(*AB[0], 0))

    answer(ans)


def main():
    N = NI()
    AB = EI(N)

    ans = []

    CENTER = 5 * 10**17

    # 二次元平面上で考える
    # 操作は「2点選んで中点につぶす」
    # 4点→2点→1点にした点がAB[0]と反対側ならOK

    # AB[0]が端の場合無理なのでまず1回やる
    minm = CENTER * 2
    nj = 1
    for j in range(1, N):
        m = manhattan(*AB[0], *AB[j])
        if m < minm:
            nj = j
            minm = m
    query(AB, 0, nj)
    ans.append([0, nj])

    for _ in range(3):

        # 0以外の4点を全探索
        a0, b0 = AB[0]
        ta, tb = CENTER*2 - a0, CENTER*2 - b0

        minm = CENTER * 2
        bestP = []
        for P in permutations(range(1, N), 4):
            p1, p2, p3, p4 = P
            na1, nb1 = means(AB, p1, p2)
            na2, nb2 = means(AB, p3, p4)
            na, nb = (na1+na2) // 2, (nb1+nb2) // 2
            m = manhattan(ta, tb, na, nb)
            if m < minm:
                minm = m
                bestP = list(P)

        for P in permutations(range(1, N), 3):
            p1, p2, p3 = P
            na1, nb1 = means(AB, p1, p2)
            na2, nb2 = AB[p3]
            na, nb = (na1+na2) // 2, (nb1+nb2) // 2
            m = manhattan(ta, tb, na, nb)
            if m < minm:
                minm = m
                bestP = list(P)

        # print(score(*AB[0], 0))

        if len(bestP) == 3:
            p1, p2, p3 = bestP
            query(AB, p1, p2)
            ans.append([p1, p2])
            query(AB, p2, p3)
            ans.append([p2, p3])
            query(AB, p3, 0)
            ans.append([p3, 0])

        elif len(bestP) == 4:
            p1, p2, p3, p4 = bestP
            query(AB, p1, p2)
            ans.append([p1, p2])
            query(AB, p3, p4)
            ans.append([p3, p4])
            query(AB, p2, p4)
            ans.append([p2, p4])
            query(AB, 0, p4)
            ans.append([0, p4])

    answer(ans)
    # print(score(*AB[0], 1))
    # print(AB[0])


def sort_ABs():
    # いまいち
    N = NI()
    AB = EI(N)

    ans = []

    ABI = [[a, b, i] for i, (a, b) in enumerate(AB)]
    ABI.sort()
    for si in range(N//2):
        i, j = ABI[si][2], ABI[N-1-si][2]
        query(AB, i, j)
        ans.append([i, j])
    ABI = [[a, b, i] for i, (a, b) in enumerate(AB)]
    AB.sort(key=lambda x: x[1])
    for si in range(N // 2):
        i, j = ABI[si][2], ABI[N - 1 - si][2]
        query(AB, i, j)
        ans.append([i, j])

    CENTER = 5 * 10**17
    a0, b0 = AB[0]
    ta = CENTER * 2 - a0
    tb = CENTER * 2 - b0

    minm = CENTER * 2
    best_ij = [0, 1]
    for i in range(1, N):
        for j in range(i + 1, N):
            na, nb = means(AB, i, j)
            m = manhattan(na, nb, ta, tb)
            if m < minm:
                best_ij = [i, j]
                minm = m

    query(AB, *best_ij)
    ans.append(best_ij)

    j = best_ij[1]
    query(AB, 0, j)
    ans.append([0, j])

    answer(ans)


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