結果

問題 No.2315 Flying Camera
ユーザー rin204rin204
提出日時 2023-05-26 23:57:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,647 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 82,480 KB
実行使用メモリ 69,888 KB
最終ジャッジ日時 2024-06-07 09:34:41
合計ジャッジ時間 2,557 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,864 KB
testcase_01 AC 41 ms
53,376 KB
testcase_02 AC 42 ms
52,736 KB
testcase_03 AC 61 ms
64,896 KB
testcase_04 AC 66 ms
67,328 KB
testcase_05 AC 64 ms
66,432 KB
testcase_06 AC 53 ms
62,592 KB
testcase_07 AC 65 ms
67,584 KB
testcase_08 AC 51 ms
60,928 KB
testcase_09 AC 42 ms
53,504 KB
testcase_10 AC 53 ms
61,696 KB
testcase_11 AC 68 ms
67,584 KB
testcase_12 AC 43 ms
53,760 KB
testcase_13 AC 41 ms
52,864 KB
testcase_14 AC 69 ms
67,968 KB
testcase_15 AC 61 ms
64,768 KB
testcase_16 AC 57 ms
62,848 KB
testcase_17 AC 50 ms
60,160 KB
testcase_18 AC 71 ms
69,888 KB
testcase_19 AC 61 ms
65,280 KB
testcase_20 AC 47 ms
59,392 KB
testcase_21 AC 56 ms
63,744 KB
testcase_22 AC 43 ms
54,272 KB
testcase_23 AC 41 ms
52,864 KB
testcase_24 AC 70 ms
69,760 KB
testcase_25 AC 40 ms
52,864 KB
testcase_26 AC 63 ms
66,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


class Heapq:
    def __init__(self, lst=[], reverse=False):
        if reverse:
            self.pm = -1
            self.hq = [-l for l in lst]
        else:
            self.pm = 1
            self.hq = lst.copy()
        heapq.heapify(self.hq)
        self.tot = sum(lst)
        self.rm = []
        self.length = len(lst)

    def push(self, x):
        self.length += 1
        heapq.heappush(self.hq, x * self.pm)
        self.tot += x

    def pop(self):
        self.length -= 1
        ret = heapq.heappop(self.hq)
        self.tot -= self.pm * ret
        self.delete()
        return self.pm * ret

    def front(self):
        return self.pm * self.hq[0]

    def remove(self, x):  # 存在しないものを消そうとするとバグる
        self.length -= 1
        self.tot -= x
        heapq.heappush(self.rm, self.pm * x)
        self.delete()

    def delete(self):
        while self.rm and self.rm[0] == self.hq[0]:
            heapq.heappop(self.rm)
            heapq.heappop(self.hq)


class Med_lst:
    def __init__(self, lst=[]):
        inf = 1 << 60
        l = len(lst)
        self.low = Heapq(lst=[-inf] + lst[: (l + 1) // 2], reverse=True)
        self.upp = Heapq(lst=[inf] + lst[(l + 1) // 2 :])
        self.low.tot += inf
        self.upp.tot -= inf
        self.low.length -= 1
        self.upp.length -= 1

    #    def get(self):
    #        return self.low.front()

    def get(self):
        if self.low.length == self.upp.length:
            return (self.low.front() + self.upp.front()) // 2
        else:
            return self.low.front()

    def abs_sum(self):
        med = self.low.front()
        ret = med * self.low.length - self.low.tot
        ret += self.upp.tot - med * self.upp.length
        return ret

    def push(self, x):
        lst = [self.low.pop(), x, self.upp.pop()]
        lst.sort()
        if self.low.length == self.upp.length:
            self.low.push(lst[0])
            self.low.push(lst[1])
            self.upp.push(lst[2])
        else:
            self.low.push(lst[0])
            self.upp.push(lst[1])
            self.upp.push(lst[2])

    def remove(self, x):
        med = self.get()
        if x <= med:
            self.low.remove(x)
            if self.low.length < self.upp.length:
                self.low.push(self.upp.pop())
        else:
            self.upp.remove(x)
            if self.low.length > self.upp.length + 1:
                self.upp.push(self.low.pop())


n = int(input())
X = Med_lst()
Y = Med_lst()
for _ in range(n):
    x, y = map(int, input().split())
    X.push(x)
    Y.push(y)

print(X.abs_sum() + Y.abs_sum())
0