結果

問題 No.5020 Averaging
ユーザー Mao-betaMao-beta
提出日時 2024-02-25 16:32:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 614 ms / 1,000 ms
コード長 7,029 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 76,680 KB
スコア 28,922,398
最終ジャッジ日時 2024-02-25 16:33:04
合計ジャッジ時間 28,489 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 568 ms
76,464 KB
testcase_01 AC 518 ms
76,452 KB
testcase_02 AC 479 ms
76,524 KB
testcase_03 AC 587 ms
76,476 KB
testcase_04 AC 435 ms
76,524 KB
testcase_05 AC 509 ms
76,432 KB
testcase_06 AC 502 ms
76,424 KB
testcase_07 AC 550 ms
76,552 KB
testcase_08 AC 517 ms
76,316 KB
testcase_09 AC 549 ms
76,452 KB
testcase_10 AC 541 ms
76,444 KB
testcase_11 AC 497 ms
76,424 KB
testcase_12 AC 516 ms
76,444 KB
testcase_13 AC 472 ms
76,400 KB
testcase_14 AC 492 ms
76,300 KB
testcase_15 AC 496 ms
76,424 KB
testcase_16 AC 496 ms
76,400 KB
testcase_17 AC 485 ms
76,312 KB
testcase_18 AC 574 ms
76,444 KB
testcase_19 AC 516 ms
76,424 KB
testcase_20 AC 546 ms
76,444 KB
testcase_21 AC 540 ms
76,400 KB
testcase_22 AC 459 ms
76,424 KB
testcase_23 AC 465 ms
76,524 KB
testcase_24 AC 474 ms
76,424 KB
testcase_25 AC 574 ms
76,444 KB
testcase_26 AC 524 ms
76,680 KB
testcase_27 AC 473 ms
76,396 KB
testcase_28 AC 564 ms
76,680 KB
testcase_29 AC 482 ms
76,400 KB
testcase_30 AC 599 ms
76,452 KB
testcase_31 AC 528 ms
76,448 KB
testcase_32 AC 556 ms
76,460 KB
testcase_33 AC 509 ms
76,400 KB
testcase_34 AC 614 ms
76,584 KB
testcase_35 AC 456 ms
76,296 KB
testcase_36 AC 451 ms
76,524 KB
testcase_37 AC 448 ms
76,396 KB
testcase_38 AC 553 ms
76,444 KB
testcase_39 AC 518 ms
76,424 KB
testcase_40 AC 540 ms
76,316 KB
testcase_41 AC 553 ms
76,316 KB
testcase_42 AC 536 ms
76,316 KB
testcase_43 AC 497 ms
76,424 KB
testcase_44 AC 533 ms
76,424 KB
testcase_45 AC 506 ms
76,400 KB
testcase_46 AC 512 ms
76,412 KB
testcase_47 AC 530 ms
76,452 KB
testcase_48 AC 483 ms
76,296 KB
testcase_49 AC 453 ms
76,652 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 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(2):

        # 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