Neterukun's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Neterukun1993/Library

:heavy_check_mark: 動的中央値クエリ
(DataStructure/misc/DynamicMedian.py)

概要

DynamicMedian: 集合の中央値を動的に管理するデータ構造

使い方

DynamicMedian()
中央値を管理する空の集合を構築する。計算量 $O(1)$

Verified with

Code

# from heapq import heappushpop, heappush
from standard_library.heapq import heappushpop, heappush


class DynamicMedian:
    def __init__(self):
        self.l_q = []
        self.r_q = []
        self.l_sum = 0
        self.r_sum = 0

    def add(self, val):
        if len(self.l_q) == len(self.r_q):
            self.l_sum += val
            val = -heappushpop(self.l_q, -val)
            self.l_sum -= val
            heappush(self.r_q, val)
            self.r_sum += val
        else:
            self.r_sum += val
            val = heappushpop(self.r_q, val)
            self.r_sum -= val
            heappush(self.l_q, -val)
            self.l_sum += val

    def median_low(self):
        if len(self.l_q) + 1 == len(self.r_q):
            return self.r_q[0]
        else:
            return -self.l_q[0]

    def median_high(self):
        return self.r_q[0]

    def minimum_query(self):
        """min(|x - a_1| + |x - a_2| + ⋯ + |x - a_N|) s.t. any x"""
        res1 = (len(self.l_q) * self.median_high() - self.l_sum)
        res2 = (self.r_sum - len(self.r_q) * self.median_high())
        return res1 + res2
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/python.py", line 96, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page