提出 #53003393


ソースコード 拡げる

# https://github.com/tatyam-prime/SortedSet/blob/main/SortedSet.py
import math
from bisect import bisect_left, bisect_right
from typing import Generic, Iterable, Iterator, List, Tuple, TypeVar, Optional
T = TypeVar('T')

class SortedSet(Generic[T]):
    BUCKET_RATIO = 16
    SPLIT_RATIO = 24
    
    def __init__(self, a: Iterable[T] = []) -> None:
        "Make a new SortedSet from iterable. / O(N) if sorted and unique / O(N log N)"
        a = list(a)
        n = len(a)
        if any(a[i] > a[i + 1] for i in range(n - 1)):
            a.sort()
        if any(a[i] >= a[i + 1] for i in range(n - 1)):
            a, b = [], a
            for x in b:
                if not a or a[-1] != x:
                    a.append(x)
        n = self.size = len(a)
        num_bucket = int(math.ceil(math.sqrt(n / self.BUCKET_RATIO)))
        self.a = [a[n * i // num_bucket : n * (i + 1) // num_bucket] for i in range(num_bucket)]

    def __iter__(self) -> Iterator[T]:
        for i in self.a:
            for j in i: yield j

    def __reversed__(self) -> Iterator[T]:
        for i in reversed(self.a):
            for j in reversed(i): yield j
    
    def __eq__(self, other) -> bool:
        return list(self) == list(other)
    
    def __len__(self) -> int:
        return self.size
    
    def __repr__(self) -> str:
        return "SortedSet" + str(self.a)
    
    def __str__(self) -> str:
        s = str(list(self))
        return "{" + s[1 : len(s) - 1] + "}"

    def _position(self, x: T) -> Tuple[List[T], int, int]:
        "return the bucket, index of the bucket and position in which x should be. self must not be empty."
        for i, a in enumerate(self.a):
            if x <= a[-1]: break
        return (a, i, bisect_left(a, x))

    def __contains__(self, x: T) -> bool:
        if self.size == 0: return False
        a, _, i = self._position(x)
        return i != len(a) and a[i] == x

    def add(self, x: T) -> bool:
        "Add an element and return True if added. / O(√N)"
        if self.size == 0:
            self.a = [[x]]
            self.size = 1
            return True
        a, b, i = self._position(x)
        if i != len(a) and a[i] == x: return False
        a.insert(i, x)
        self.size += 1
        if len(a) > len(self.a) * self.SPLIT_RATIO:
            mid = len(a) >> 1
            self.a[b:b+1] = [a[:mid], a[mid:]]
        return True
    
    def _pop(self, a: List[T], b: int, i: int) -> T:
        ans = a.pop(i)
        self.size -= 1
        if not a: del self.a[b]
        return ans

    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0: return False
        a, b, i = self._position(x)
        if i == len(a) or a[i] != x: return False
        self._pop(a, b, i)
        return True
    
    def lt(self, x: T) -> Optional[T]:
        "Find the largest element < x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] < x:
                return a[bisect_left(a, x) - 1]

    def le(self, x: T) -> Optional[T]:
        "Find the largest element <= x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] <= x:
                return a[bisect_right(a, x) - 1]

    def gt(self, x: T) -> Optional[T]:
        "Find the smallest element > x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] > x:
                return a[bisect_right(a, x)]

    def ge(self, x: T) -> Optional[T]:
        "Find the smallest element >= x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] >= x:
                return a[bisect_left(a, x)]
    
    def __getitem__(self, i: int) -> T:
        "Return the i-th element."
        if i < 0:
            for a in reversed(self.a):
                i += len(a)
                if i >= 0: return a[i]
        else:
            for a in self.a:
                if i < len(a): return a[i]
                i -= len(a)
        raise IndexError
    
    def pop(self, i: int = -1) -> T:
        "Pop and return the i-th element."
        if i < 0:
            for b, a in enumerate(reversed(self.a)):
                i += len(a)
                if i >= 0: return self._pop(a, ~b, i)
        else:
            for b, a in enumerate(self.a):
                if i < len(a): return self._pop(a, b, i)
                i -= len(a)
        raise IndexError
    
    def index(self, x: T) -> int:
        "Count the number of elements < x."
        ans = 0
        for a in self.a:
            if a[-1] >= x:
                return ans + bisect_left(a, x)
            ans += len(a)
        return ans

    def index_right(self, x: T) -> int:
        "Count the number of elements <= x."
        ans = 0
        for a in self.a:
            if a[-1] > x:
                return ans + bisect_right(a, x)
            ans += len(a)
        return ans


# https://github.com/tatyam-prime/SortedSet/blob/main/SortedMultiset.py
import math
from bisect import bisect_left, bisect_right
from typing import Generic, Iterable, Iterator, List, Tuple, TypeVar, Optional
T = TypeVar('T')

class SortedMultiset(Generic[T]):
    BUCKET_RATIO = 16
    SPLIT_RATIO = 24
    
    def __init__(self, a: Iterable[T] = []) -> None:
        "Make a new SortedMultiset from iterable. / O(N) if sorted / O(N log N)"
        a = list(a)
        n = self.size = len(a)
        if any(a[i] > a[i + 1] for i in range(n - 1)):
            a.sort()
        num_bucket = int(math.ceil(math.sqrt(n / self.BUCKET_RATIO)))
        self.a = [a[n * i // num_bucket : n * (i + 1) // num_bucket] for i in range(num_bucket)]

    def __iter__(self) -> Iterator[T]:
        for i in self.a:
            for j in i: yield j

    def __reversed__(self) -> Iterator[T]:
        for i in reversed(self.a):
            for j in reversed(i): yield j
    
    def __eq__(self, other) -> bool:
        return list(self) == list(other)
    
    def __len__(self) -> int:
        return self.size
    
    def __repr__(self) -> str:
        return "SortedMultiset" + str(self.a)
    
    def __str__(self) -> str:
        s = str(list(self))
        return "{" + s[1 : len(s) - 1] + "}"

    def _position(self, x: T) -> Tuple[List[T], int, int]:
        "return the bucket, index of the bucket and position in which x should be. self must not be empty."
        for i, a in enumerate(self.a):
            if x <= a[-1]: break
        return (a, i, bisect_left(a, x))

    def __contains__(self, x: T) -> bool:
        if self.size == 0: return False
        a, _, i = self._position(x)
        return i != len(a) and a[i] == x

    def count(self, x: T) -> int:
        "Count the number of x."
        return self.index_right(x) - self.index(x)

    def add(self, x: T) -> None:
        "Add an element. / O(√N)"
        if self.size == 0:
            self.a = [[x]]
            self.size = 1
            return
        a, b, i = self._position(x)
        a.insert(i, x)
        self.size += 1
        if len(a) > len(self.a) * self.SPLIT_RATIO:
            mid = len(a) >> 1
            self.a[b:b+1] = [a[:mid], a[mid:]]
    
    def _pop(self, a: List[T], b: int, i: int) -> T:
        ans = a.pop(i)
        self.size -= 1
        if not a: del self.a[b]
        return ans

    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0: return False
        a, b, i = self._position(x)
        if i == len(a) or a[i] != x: return False
        self._pop(a, b, i)
        return True

    def lt(self, x: T) -> Optional[T]:
        "Find the largest element < x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] < x:
                return a[bisect_left(a, x) - 1]

    def le(self, x: T) -> Optional[T]:
        "Find the largest element <= x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] <= x:
                return a[bisect_right(a, x) - 1]

    def gt(self, x: T) -> Optional[T]:
        "Find the smallest element > x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] > x:
                return a[bisect_right(a, x)]

    def ge(self, x: T) -> Optional[T]:
        "Find the smallest element >= x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] >= x:
                return a[bisect_left(a, x)]
    
    def __getitem__(self, i: int) -> T:
        "Return the i-th element."
        if i < 0:
            for a in reversed(self.a):
                i += len(a)
                if i >= 0: return a[i]
        else:
            for a in self.a:
                if i < len(a): return a[i]
                i -= len(a)
        raise IndexError
    
    def pop(self, i: int = -1) -> T:
        "Pop and return the i-th element."
        if i < 0:
            for b, a in enumerate(reversed(self.a)):
                i += len(a)
                if i >= 0: return self._pop(a, ~b, i)
        else:
            for b, a in enumerate(self.a):
                if i < len(a): return self._pop(a, b, i)
                i -= len(a)
        raise IndexError

    def index(self, x: T) -> int:
        "Count the number of elements < x."
        ans = 0
        for a in self.a:
            if a[-1] >= x:
                return ans + bisect_left(a, x)
            ans += len(a)
        return ans

    def index_right(self, x: T) -> int:
        "Count the number of elements <= x."
        ans = 0
        for a in self.a:
            if a[-1] > x:
                return ans + bisect_right(a, x)
            ans += len(a)
        return ans



# https://github.com/prd-xxx/gorichan_kyopro_library/blob/main/top_k_sorted_multiset/top_k_sorted_multiset.py
class TopKSortedMultiset:
    def __init__(self, k, arr:Iterable[T] = [], mode_max = True) -> None:
        assert k >= 0
        self.topk = SortedMultiset()
        self.other = SortedMultiset()
        self.k = k
        self.mode_max = mode_max
        self.sum_topk = self.sum_other = 0
        for a in arr:
            self.add(a)
    def _balance(self):
        while len(self.topk) > self.k:
            x = self.topk.pop(0 if self.mode_max else -1)
            self.sum_topk -= x
            self.other.add(x)
            self.sum_other += x
        while len(self.topk) < self.k and self.other:
            x = self.other.pop(-1 if self.mode_max else 0)
            self.sum_other -= x
            self.topk.add(x)
            self.sum_topk += x
    def add(self, x):
        self.topk.add(x)
        self.sum_topk += x
        self._balance()
    def discard(self, x):
        if x in self.topk:
            assert self.topk.discard(x)
            self.sum_topk -= x
        else:
            assert self.other.discard(x)
            self.sum_other -= x
        self._balance()
    def __len__(self):
        return len(self.topk) + len(self.other)
    def __str__(self):
        return str(self.topk) + " | " + str(self.other)


N,K = map(int,input().split())
C = list(map(int,input().split()))
Q = int(input())
qs = [C[int(input())-1] for i in range(Q)]

ss = SortedSet(C)
ini_arr = []
p = None
for c in ss:
    if p is not None:
        ini_arr.append(c-p)
    p = c
ts = TopKSortedMultiset(K-1, ini_arr)
print(ts.sum_other)

for q in qs:
    ss.discard(q)
    l = ss.lt(q)
    r = ss.gt(q)
    if l is None:
        ts.discard(r-q)
    elif r is None:
        ts.discard(q-l)
    else:
        ts.discard(r-q)
        ts.discard(q-l)
        ts.add(r-l)
    print(ts.sum_other)

提出情報

提出日時
問題 D - ドーナツの箱詰め
ユーザ prd_xxx
言語 Python (PyPy 3.10-v7.3.12)
得点 100
コード長 11941 Byte
結果 AC
実行時間 1853 ms
メモリ 120180 KiB

ジャッジ結果

セット名 Sample Subtask1 Subtask2 Subtask3 Subtask4
得点 / 配点 0 / 0 15 / 15 25 / 25 30 / 30 30 / 30
結果
AC × 3
AC × 5
AC × 8
AC × 10
AC × 38
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
Subtask1 subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt
Subtask2 subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt
Subtask3 subtask3_01.txt, subtask3_02.txt, subtask3_03.txt, subtask3_04.txt, subtask3_05.txt, subtask3_06.txt, subtask3_07.txt, subtask3_08.txt, subtask3_09.txt, subtask3_10.txt
Subtask4 sample_01.txt, sample_02.txt, sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt, subtask3_04.txt, subtask3_05.txt, subtask3_06.txt, subtask3_07.txt, subtask3_08.txt, subtask3_09.txt, subtask3_10.txt, subtask4_01.txt, subtask4_02.txt, subtask4_03.txt, subtask4_04.txt, subtask4_05.txt, subtask4_06.txt, subtask4_07.txt, subtask4_08.txt, subtask4_09.txt, subtask4_10.txt, subtask4_11.txt, subtask4_12.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 123 ms 85348 KiB
sample_02.txt AC 128 ms 85464 KiB
sample_03.txt AC 125 ms 85536 KiB
subtask1_01.txt AC 125 ms 85632 KiB
subtask1_02.txt AC 134 ms 86212 KiB
subtask1_03.txt AC 249 ms 106204 KiB
subtask1_04.txt AC 289 ms 119924 KiB
subtask1_05.txt AC 291 ms 119984 KiB
subtask2_01.txt AC 129 ms 85524 KiB
subtask2_02.txt AC 275 ms 119676 KiB
subtask2_03.txt AC 250 ms 119804 KiB
subtask2_04.txt AC 823 ms 120180 KiB
subtask2_05.txt AC 317 ms 114140 KiB
subtask2_06.txt AC 243 ms 104316 KiB
subtask2_07.txt AC 735 ms 120068 KiB
subtask2_08.txt AC 655 ms 119564 KiB
subtask3_01.txt AC 124 ms 85652 KiB
subtask3_02.txt AC 1340 ms 114284 KiB
subtask3_03.txt AC 354 ms 97188 KiB
subtask3_04.txt AC 1440 ms 118096 KiB
subtask3_05.txt AC 525 ms 117980 KiB
subtask3_06.txt AC 580 ms 118104 KiB
subtask3_07.txt AC 539 ms 117976 KiB
subtask3_08.txt AC 447 ms 104396 KiB
subtask3_09.txt AC 469 ms 114144 KiB
subtask3_10.txt AC 1236 ms 118072 KiB
subtask4_01.txt AC 395 ms 102672 KiB
subtask4_02.txt AC 759 ms 117964 KiB
subtask4_03.txt AC 1646 ms 118404 KiB
subtask4_04.txt AC 249 ms 119396 KiB
subtask4_05.txt AC 1838 ms 118276 KiB
subtask4_06.txt AC 752 ms 118224 KiB
subtask4_07.txt AC 1548 ms 118104 KiB
subtask4_08.txt AC 1853 ms 118280 KiB
subtask4_09.txt AC 581 ms 117824 KiB
subtask4_10.txt AC 394 ms 99100 KiB
subtask4_11.txt AC 530 ms 106008 KiB
subtask4_12.txt AC 734 ms 118068 KiB