Problem: Takahashi is hosting an sports meet. There are N people who will participate. These people are conveniently numbered 1 through N. Also, there are M options of sports for this event. These sports are numbered 1 through M. Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.  Takahashi knows that Person i's j-th favorite sport is Sport A_{ij}. Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.  Takahashi is worried that one of the sports will attract too many people. Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized. Find the minimum possible number of the participants in the sport with the largest number of participants.

Submission attempts (in order):
from collections import deque
from typing import Optional


class DeletableDeque:
    """Deletable Deque: a deque that supports lazy deletion of values."""

    __slots__ = ["_set", "_deque"]

    def __init__(self, *initial_values: int) -> None:
        self._set = set()
        self._deque = deque([])

        if initial_values:
            for v in initial_values:
                self.append(v)

    def __contains__(self, value: int) -> bool:
        return value in self._set

    def __len__(self) -> int:
        return len(self._set)

    @property
    def is_empty(self) -> bool:
        """Return whether the deque has no values or not."""
        return len(self) == 0

    @property
    def top(self) -> Optional[int]:
        """Return the rightmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[-1] not in self._set:
            self._deque.pop()
        if not self._deque:
            return None
        return self._deque[-1]

    @property
    def topleft(self) -> Optional[int]:
        """Return the leftmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[0] not in self._set:
            self._deque.popleft()
        if not self._deque:
            return None
        return self._deque[0]

    def pop(self) -> Optional[int]:
        """Pop and return the rightmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[-1] not in self._set:
            self._deque.pop()
        if not self._deque:
            return None
        res = self._deque.pop()
        self._set.discard(res)
        return res

    def popleft(self) -> Optional[int]:
        """Pop and return the leftmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[0] not in self._set:
            self._deque.popleft()
        if not self._deque:
            return None
        res = self._deque.popleft()
        self._set.discard(res)
        return res

    def append(self, value: int) -> None:
        """Append value to the right side of the deque."""
        if value in self._set:
            return
        self._set.add(value)
        self._deque.append(value)

    def appendleft(self, value: int) -> None:
        """Append value to the left side of the deque."""
        if value in self._set:
            return
        self._set.add(value)
        self._deque.appendleft(value)

    def delete(self, value: int) -> None:
        """Delete value from the deque."""
        if value not in self._set:
            return
        self._set.discard(value)


def agc018_b():
    
    import sys
    from collections import defaultdict

    readline = sys.stdin.buffer.readline

    N, M = map(int, readline().split())
    A = []
    for _ in range(N):
        ranking = [(x, i) for i, x in enumerate(map(int, readline().split()), 1)]
        ranking.sort()
        _, fav_sports = zip(*ranking)
        print(fav_sports)
        A.append(DeletableDeque(*fav_sports))

    res = 301
    for _ in range(M):
        cnt = defaultdict(int)
        for a in A:
            cnt[a.topleft] += 1

        popular_sport = max(cnt, key=cnt.get)
        res = min(res, cnt[popular_sport])
        print(popular_sport, cnt[popular_sport])
        for a in A:
            a.delete(popular_sport)

    print(res)


if __name__ == "__main__":
    agc018_b()


from collections import deque
from typing import Optional


class DeletableDeque:
    """Deletable Deque: a deque that supports lazy deletion of values."""

    __slots__ = ["_set", "_deque"]

    def __init__(self, *initial_values: int) -> None:
        self._set = set()
        self._deque = deque([])

        if initial_values:
            for v in initial_values:
                self.append(v)

    def __contains__(self, value: int) -> bool:
        return value in self._set

    def __len__(self) -> int:
        return len(self._set)

    @property
    def is_empty(self) -> bool:
        """Return whether the deque has no values or not."""
        return len(self) == 0

    @property
    def top(self) -> Optional[int]:
        """Return the rightmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[-1] not in self._set:
            self._deque.pop()
        if not self._deque:
            return None
        return self._deque[-1]

    @property
    def topleft(self) -> Optional[int]:
        """Return the leftmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[0] not in self._set:
            self._deque.popleft()
        if not self._deque:
            return None
        return self._deque[0]

    def pop(self) -> Optional[int]:
        """Pop and return the rightmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[-1] not in self._set:
            self._deque.pop()
        if not self._deque:
            return None
        res = self._deque.pop()
        self._set.discard(res)
        return res

    def popleft(self) -> Optional[int]:
        """Pop and return the leftmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[0] not in self._set:
            self._deque.popleft()
        if not self._deque:
            return None
        res = self._deque.popleft()
        self._set.discard(res)
        return res

    def append(self, value: int) -> None:
        """Append value to the right side of the deque."""
        if value in self._set:
            return
        self._set.add(value)
        self._deque.append(value)

    def appendleft(self, value: int) -> None:
        """Append value to the left side of the deque."""
        if value in self._set:
            return
        self._set.add(value)
        self._deque.appendleft(value)

    def delete(self, value: int) -> None:
        """Delete value from the deque."""
        if value not in self._set:
            return
        self._set.discard(value)


def agc018_b():
    
    import sys
    from collections import defaultdict

    readline = sys.stdin.buffer.readline

    N, M = map(int, readline().split())
    A = []
    for _ in range(N):
        ranking = [(x, i) for i, x in enumerate(map(int, readline().split()), 1)]
        ranking.sort()
        _, fav_sports = zip(*ranking)
        A.append(DeletableDeque(*fav_sports))

    res = 301
    for _ in range(M):
        cnt = defaultdict(int)
        for a in A:
            cnt[a.topleft] += 1

        popular_sport = max(cnt, key=cnt.get)
        res = min(res, cnt[popular_sport])
        for a in A:
            a.delete(popular_sport)

    print(res)


if __name__ == "__main__":
    agc018_b()


from collections import deque
from typing import Optional


class DeletableDeque:
    """Deletable Deque: a deque that supports lazy deletion of values."""

    __slots__ = ["_set", "_deque"]

    def __init__(self, *initial_values: int) -> None:
        self._set = set()
        self._deque = deque([])

        if initial_values:
            for v in initial_values:
                self.append(v)

    def __contains__(self, value: int) -> bool:
        return value in self._set

    def __len__(self) -> int:
        return len(self._set)

    @property
    def is_empty(self) -> bool:
        """Return whether the deque has no values or not."""
        return len(self) == 0

    @property
    def top(self) -> Optional[int]:
        """Return the rightmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[-1] not in self._set:
            self._deque.pop()
        if not self._deque:
            return None
        return self._deque[-1]

    @property
    def topleft(self) -> Optional[int]:
        """Return the leftmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[0] not in self._set:
            self._deque.popleft()
        if not self._deque:
            return None
        return self._deque[0]

    def pop(self) -> Optional[int]:
        """Pop and return the rightmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[-1] not in self._set:
            self._deque.pop()
        if not self._deque:
            return None
        res = self._deque.pop()
        self._set.discard(res)
        return res

    def popleft(self) -> Optional[int]:
        """Pop and return the leftmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[0] not in self._set:
            self._deque.popleft()
        if not self._deque:
            return None
        res = self._deque.popleft()
        self._set.discard(res)
        return res

    def append(self, value: int) -> None:
        """Append value to the right side of the deque."""
        if value in self._set:
            return
        self._set.add(value)
        self._deque.append(value)

    def appendleft(self, value: int) -> None:
        """Append value to the left side of the deque."""
        if value in self._set:
            return
        self._set.add(value)
        self._deque.appendleft(value)

    def delete(self, value: int) -> None:
        """Delete value from the deque."""
        if value not in self._set:
            return
        self._set.discard(value)


def agc018_b():
    
    import sys
    from collections import defaultdict

    readline = sys.stdin.buffer.readline

    N, M = map(int, readline().split())
    A = []
    for _ in range(N):
        ranking = [(x, i) for i, x in enumerate(map(int, readline().split()), 1)]
        ranking.sort()
        _, fav_sports = zip(*ranking)
        A.append(DeletableDeque(*fav_sports))

    res = 301
    for _ in range(M):
        cnt = defaultdict(int)
        for a in A:
            cnt[a.topleft] += 1

        popular_sport = max(cnt, key=cnt.get)
        res = min(res, cnt[popular_sport])
        for a in A:
            a.delete(popular_sport)

    print(res)


if __name__ == "__main__":
    agc018_b()


from collections import deque
from typing import Optional


class DeletableDeque:
    """Deletable Deque: a deque that supports lazy deletion of values."""

    __slots__ = ["_set", "_deque"]

    def __init__(self, *initial_values: int) -> None:
        self._set = set()
        self._deque = deque([])

        if initial_values:
            for v in initial_values:
                self.append(v)

    @property
    def topleft(self) -> Optional[int]:
        """Return the leftmost value from the deque.
        If the deque is empty, None is returned.
        """
        while self._deque and self._deque[0] not in self._set:
            self._deque.popleft()
        if not self._deque:
            return None
        return self._deque[0]

    def append(self, value: int) -> None:
        """Append value to the right side of the deque."""
        if value in self._set:
            return
        self._set.add(value)
        self._deque.append(value)

    def delete(self, value: int) -> None:
        """Delete value from the deque."""
        if value not in self._set:
            return
        self._set.discard(value)


def agc018_b():
    
    import sys
    from collections import Counter

    readline = sys.stdin.buffer.readline

    N, M = map(int, readline().split())
    A = [DeletableDeque(*map(int, readline().split())) for _ in range(N)]

    res = 301
    for _ in range(M):
        cnt = Counter(a.topleft for a in A)
        popular_sport = max(cnt, key=cnt.get)
        res = min(res, cnt[popular_sport])
        for a in A:
            a.delete(popular_sport)

    print(res)


if __name__ == "__main__":
    agc018_b()


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted