提出 #33285610


ソースコード 拡げる

# 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, TypeVar, Union, List
T = TypeVar('T')

class SortedSet(Generic[T]):
    BUCKET_RATIO = 50
    REBUILD_RATIO = 170

    def _build(self, a=None) -> None:
        "Evenly divide `a` into buckets."
        if a is None: a = list(self)
        size = self.size = len(a)
        bucket_size = int(math.ceil(math.sqrt(size / self.BUCKET_RATIO)))
        self.a = [a[size * i // bucket_size : size * (i + 1) // bucket_size] for i in range(bucket_size)]
    
    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)
        if not all(a[i] < a[i + 1] for i in range(len(a) - 1)):
            a = sorted(set(a))
        self._build(a)

    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 __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 _find_bucket(self, x: T) -> List[T]:
        "Find the bucket which should contain x. self must not be empty."
        for a in self.a:
            if x <= a[-1]: return a
        return a

    def __contains__(self, x: T) -> bool:
        if self.size == 0: return False
        a = self._find_bucket(x)
        i = bisect_left(a, 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 = self._find_bucket(x)
        i = bisect_left(a, 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.REBUILD_RATIO:
            self._build()
        return True

    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0: return False
        a = self._find_bucket(x)
        i = bisect_left(a, x)
        if i == len(a) or a[i] != x: return False
        a.pop(i)
        self.size -= 1
        if len(a) == 0: self._build()
        return True
    
    def lt(self, x: T) -> Union[T, None]:
        "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) -> Union[T, None]:
        "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) -> Union[T, None]:
        "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) -> Union[T, None]:
        "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, x: int) -> T:
        "Return the x-th element, or IndexError if it doesn't exist."
        if x < 0: x += self.size
        if x < 0: raise IndexError
        for a in self.a:
            if x < len(a): return a[x]
            x -= 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

################################

n,k=map(int,input().split())
ans=[-1]*n
from collections import defaultdict
yama=defaultdict(list)
s=SortedSet()

for i,x in enumerate(map(int,input().split())):
	key=s.ge(x)
	if key is not None:
		yama[x]=yama.pop(key)
		s.discard(key)
	yama[x].append(x)
	s.add(x)
	if len(yama[x])==k:
		for y in yama.pop(x):
			ans[y-1]=i+1
		s.discard(x)
print(*ans)

提出情報

提出日時
問題 D - Draw Your Cards
ユーザ kyopro_friends
言語 PyPy3 (7.3.0)
得点 400
コード長 4752 Byte
結果 AC
実行時間 414 ms
メモリ 169368 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 53
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 107 ms 75036 KiB
sample_02.txt AC 90 ms 74988 KiB
sample_03.txt AC 87 ms 74796 KiB
test_01.txt AC 85 ms 74996 KiB
test_02.txt AC 95 ms 75428 KiB
test_03.txt AC 89 ms 75120 KiB
test_04.txt AC 97 ms 74992 KiB
test_05.txt AC 91 ms 75140 KiB
test_06.txt AC 205 ms 86072 KiB
test_07.txt AC 267 ms 96596 KiB
test_08.txt AC 279 ms 92208 KiB
test_09.txt AC 220 ms 90916 KiB
test_10.txt AC 221 ms 86912 KiB
test_11.txt AC 160 ms 80128 KiB
test_12.txt AC 364 ms 103360 KiB
test_13.txt AC 233 ms 90532 KiB
test_14.txt AC 382 ms 105260 KiB
test_15.txt AC 184 ms 88444 KiB
test_16.txt AC 318 ms 99108 KiB
test_17.txt AC 351 ms 100744 KiB
test_18.txt AC 402 ms 106944 KiB
test_19.txt AC 265 ms 101440 KiB
test_20.txt AC 279 ms 97404 KiB
test_21.txt AC 287 ms 100144 KiB
test_22.txt AC 400 ms 107152 KiB
test_23.txt AC 371 ms 103328 KiB
test_24.txt AC 410 ms 108400 KiB
test_25.txt AC 208 ms 96860 KiB
test_26.txt AC 328 ms 100124 KiB
test_27.txt AC 284 ms 98104 KiB
test_28.txt AC 394 ms 108076 KiB
test_29.txt AC 334 ms 102716 KiB
test_30.txt AC 294 ms 97768 KiB
test_31.txt AC 274 ms 98244 KiB
test_32.txt AC 396 ms 108572 KiB
test_33.txt AC 293 ms 100712 KiB
test_34.txt AC 407 ms 109640 KiB
test_35.txt AC 270 ms 97836 KiB
test_36.txt AC 380 ms 102296 KiB
test_37.txt AC 290 ms 97848 KiB
test_38.txt AC 403 ms 108092 KiB
test_39.txt AC 273 ms 105524 KiB
test_40.txt AC 216 ms 96500 KiB
test_41.txt AC 267 ms 99156 KiB
test_42.txt AC 400 ms 108136 KiB
test_43.txt AC 284 ms 99640 KiB
test_44.txt AC 408 ms 108552 KiB
test_45.txt AC 205 ms 96524 KiB
test_46.txt AC 414 ms 103380 KiB
test_47.txt AC 207 ms 96524 KiB
test_48.txt AC 207 ms 96640 KiB
test_49.txt AC 217 ms 108132 KiB
test_50.txt AC 350 ms 169368 KiB