Submission #73705796


Source Code Expand

def main():
    S = input()
    vals = {'A': 1, 'B': 2, 'C': 3}
    st = SortedSet([(vals[c], i) for i, c in enumerate(S)])
    ans = 0
    while len(st):
        v, i = st.pop(0)
        if v == 1:
            if st.ge((v + 1, i)) is not None:
                w, j = st.ge((v + 1, i))
                if st.ge((w + 1, j)) is not None:
                    x, k = st.ge((w + 1, j))
                    ans += 1
                    st.discard((w, j))
                    st.discard((x, k))
    print(ans)

# https://github.com/tatyam-prime/SortedSet/blob/main/codon/SortedSet.py
import math
from bisect import bisect_left, bisect_right
from typing import ClassVar, Generator, Optional

class SortedSet[T]:
    size: int
    a: list[list[T]]
    BUCKET_RATIO: ClassVar[int] = 16
    SPLIT_RATIO: ClassVar[int] = 24

    def __init__(self) -> None:
        self.size = 0
        self.a = []

    def __init__(self, a: Generator[T]) -> None:
        self.__init__(list(a))

    def __init__(self, a: list[T]) -> None:
        "Make a new SortedSet from a list. / O(N) if sorted and unique / O(N log N)"
        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) -> Generator[T]:
        for i in self.a:
            for j in i: yield j

    def __reversed__(self) -> Generator[T]:
        for i in reversed(self.a):
            for j in reversed(i): yield j
    
    def __eq__(self, other: SortedSet[T]) -> bool:
        if len(self) != len(other): return False
        for x, y in zip(self, other):
            if x != y: return False
        return True

    def __ne__(self, other: SortedSet[T]) -> bool:
        return not self.__eq__(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("index out of range")
    
    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("index out of range")
    
    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

if __name__ == "__main__":
    main()

Submission Info

Submission Time
Task D - Take ABC 2
User takahashi_uni
Language Python (Codon 0.19.3)
Score 400
Code Size 6028 Byte
Status AC
Exec Time 1555 ms
Memory 64124 KiB

Compile Error


			

			
				

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 35
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 02_corner_00.txt, 02_corner_01.txt, 02_corner_02.txt, 02_corner_03.txt, 02_corner_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 22 ms 9064 KiB
00_sample_01.txt AC 11 ms 9424 KiB
00_sample_02.txt AC 11 ms 9256 KiB
01_test_00.txt AC 11 ms 9192 KiB
01_test_01.txt AC 12 ms 9340 KiB
01_test_02.txt AC 11 ms 9288 KiB
01_test_03.txt AC 1162 ms 38504 KiB
01_test_04.txt AC 847 ms 37352 KiB
01_test_05.txt AC 1180 ms 39292 KiB
01_test_06.txt AC 1553 ms 55612 KiB
01_test_07.txt AC 1482 ms 55360 KiB
01_test_08.txt AC 1517 ms 55624 KiB
01_test_09.txt AC 1473 ms 64116 KiB
01_test_10.txt AC 1555 ms 59840 KiB
01_test_11.txt AC 1456 ms 55756 KiB
01_test_12.txt AC 1466 ms 55668 KiB
01_test_13.txt AC 1453 ms 55760 KiB
01_test_14.txt AC 1405 ms 55592 KiB
01_test_15.txt AC 1421 ms 55624 KiB
01_test_16.txt AC 1409 ms 64124 KiB
01_test_17.txt AC 1449 ms 55668 KiB
01_test_18.txt AC 1390 ms 59840 KiB
01_test_19.txt AC 1549 ms 55676 KiB
01_test_20.txt AC 1435 ms 59940 KiB
01_test_21.txt AC 1526 ms 57072 KiB
01_test_22.txt AC 1393 ms 59936 KiB
01_test_23.txt AC 1394 ms 59924 KiB
01_test_24.txt AC 1407 ms 55676 KiB
01_test_25.txt AC 1418 ms 55612 KiB
01_test_26.txt AC 1390 ms 55668 KiB
02_corner_00.txt AC 1426 ms 55400 KiB
02_corner_01.txt AC 1389 ms 59840 KiB
02_corner_02.txt AC 1342 ms 64116 KiB
02_corner_03.txt AC 1169 ms 55528 KiB
02_corner_04.txt AC 1169 ms 59936 KiB