Problem:


Score : 700 points

Problem Statement

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.

Constraints

1 \leq N \leq 300

1 \leq M \leq 300

A_{i1} , A_{i2} , ... , A_{iM} is a permutation of the integers from 1 to M.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print the minimum possible number of the participants in the sport with the largest number of participants.

Sample Input 1

4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1

Sample Output 1

2

Assume that Sports 1, 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1, Person 2 in Sport 3, Person 3 in Sport 3 and Person 4 in Sport 4.
Here, the sport with the largest number of participants is Sport 3, with two participants.
There is no way to reduce the number of participants in the sport with the largest number of participants to 1. Therefore, the answer is 2.

Sample Input 2

3 3
2 1 3
2 1 3
2 1 3

Sample Output 2

3

Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected.
Therefore, the answer is 3.



Sample input:
4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1


Sample output:
2

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(n)]
    ans = n
    st = set(i for i in range(m))
    while len(st) > 1:
        dic = {v:0 for v in st}
        for i in range(n):
            for v in a[i]:
                if v in st:
                    dic[v] += 1
                    break
        tmp = max(dic.values())
        if ans > tmp:
            ans = tmp
        for key in dic.keys():
            if dic[key] == tmp:
                st.remove(key)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def isok(k):
  b=[i[::-1] for i in a]
  while all(b):
    c=[0]*(m+1)
    flag=1
    for i in range(n):
      c[b[i][-1]]+=1
    for i in range(n):
      if c[b[i][-1]]>k:
        b[i].pop()
        flag=0
    if flag:break
  return all(b)
def bsearch(a):
  ok=n
  ng=-1
  while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if isok(mid):ok=mid
    else:ng=mid
  return ok
n,m=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(n)]
print(bsearch(a))

Submission status: Wrong Answer

Solution code:
N,M=map(int, input().split())
A=[]
for i in range(N):
  d=list(map(int, input().split()))
  A.append(d)
num=[0]*N
ans=M
S=set()
for i in range(M-1):
  cnt=[0]*M
  for j in range(N):
    for k in range(M):
      d=A[j][k]-1
      if d in S:
        continue
      else:
        cnt[d]+=1
  e=max(cnt)
  ans=min(e,ans)
  f=cnt.index(e)
  S.add(f)
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int, input().split())
A=[]
for i in range(N):
  d=list(map(int, input().split()))
  A.append(d)
S=[1]*M
num=[0]*N
ans=M
for i in range(M-1):
  cnt=[0]*M
  for j in range(N):
    d=A[j][num[j]]
    while S[d-1]==0:
      num[j]+=1
      d=A[j][num[j]]
    cnt[d-1]+=1
  e=max(cnt)
  ans=min(e,ans)
  f=cnt.index(e)
  S[f]=0
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int, input().split())
A=[]
for i in range(N):
  d=list(map(int, input().split()))
  A.append(d)
S=[1]*M
num=[0]*N
ans=M
for i in range(M):
  cnt=[0]*M
  for j in range(N):
    d=A[j][num[j]]
    while S[d-1]==0:
      num[j]+=1
      d=A[j][num[j]]
    cnt[d-1]+=1
  e=max(cnt)
  ans=min(e,ans)
  f=cnt.index(e)
  S[f]=0
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = map(int, readline().split())
    a = []
    for i in range(n):
        a.append(list(map(int, readline().split())))

    ans = INF
    for _ in range(m - 1):
        cnt = [0] * (m + 1)
        for i in range(n):
            cnt[a[i][0]] += 1
        max_cnt = max(cnt)
        ans = min(ans, max_cnt)
        max_num = cnt.index(max_cnt)
        for i in range(n):
            a[i].remove(max_num)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]

Index = [[] for i in range(M)]
Sum_list = []

for i in range(N):
    for j in range(M):
        Index[A[i][j]-1].append(j)

for j in range(M):
    Sum_list.append([j, sum(Index[j])])

Sum_list = sorted(Sum_list, key=lambda x:x[1])
ans = float('inf')
alr = set(range(M))
for k in range(M):
    cnt = defaultdict(lambda: 0)
    for i in range(N):
        for j in range(M):
            if A[i][j]-1 in alr:
                cnt[A[i][j]-1] += 1
                break
    ans = min(ans, max(cnt.values()))
    alr.remove(Sum_list[k][0])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]

Index = [[] for i in range(M)]
Sum_list = []

for i in range(N):
    for j in range(M):
        Index[A[i][j]-1].append(j)

for j in range(M):
    Sum_list.append([j, sum(Index[j])])

Sum_list = sorted(Sum_list, key=lambda x:x[1], reverse=True)
ans = float('inf')
alr = set()
for k in range(M):
    alr.add(Sum_list[k][0])
    cnt = defaultdict(lambda: 0)
    for i in range(N):
        for j in range(M):
            if A[i][j]-1 in alr:
                cnt[A[i][j]-1] += 1
                break
    ans = min(ans, max(cnt.values()))

print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[list(map(int,input().split())) for i in range(N)]
ans=N
s=set()
for i in range(M-1):
  d=[0]*M
  for j in range(N):
    for k in range(M):
      if A[j][k]-1 in s:
        continue
      else:
        d[A[j][k]-1]+=1
  ans=min(ans,max(d))
  s.add(d.index(max(d)))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = list(map(int, input().split()))
a = [None]*n
d = [None]*n
from collections import Counter
for i in range(n):
    a[i] = list(map(lambda x: int(x)-1, input().split()))
    d[i] = a[i][0]

ans = float("inf")
used = [True] * m
num = m
while True:
    c = Counter(d)
#     print(c, used)
    mv, mk = max((v,k) for k,v in c.items())
#     print(mv, mk)
    ans = min(ans, mv)
    used[mk] = False
    num -= 1
    if num==0:
        break
    
    for i in range(n):
        if not used[d[i]]:
            for j in range(m):
                if used[a[i][j]]:
                    d[i] = a[i][j]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = list(map(int, input().split()))
a = [None]*n
d = [None]*n
from collections import Counter
for i in range(n):
    a[i] = list(map(lambda x: int(x)-1, input().split()))
    d[i] = a[i][0]

ans = float("inf")
used = [True] * m
num = m
while True:
    c = Counter(d)
    mv, mk = max((v,k) for k,v in c.items())
#     print(mv, mk)
    if num==0:
        break
    ans = min(ans, mv)
    used[mk] = False
    num -= 1
    for i in range(n):
        if not used[d[i]]:
            for j in range(m):
                if used[a[i][j]]:
                    d[i] = a[i][j]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = list(map(int, input().split()))
a = [None]*n
d = [None]*n
from collections import Counter
for i in range(n):
    a[i] = list(map(lambda x: int(x)-1, input().split()))
    d[i] = a[i][0]

ans = float("inf")
used = [True] * m
num = m
while True:
    c = Counter(d)
    mv, mk = max((v,k) for k,v in c.items())
#     print(mv, mk)
    if num==0:
        break
    ans = min(ans, mv)
    used[mk] = False
    num -= 1
    for i in range(n):
        if d[i]==mk:
            for j in range(m):
                if used[a[i][j]]:
                    d[i] = a[i][j]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

n,m = list(map(int, input().split()))
a = [None]*n
d = [None]*n
from collections import Counter
for i in range(n):
    a[i] = list(map(lambda x: int(x)-1, input().split()))
    d[i] = a[i][0]

ans = float("inf")
used = [True] * m
num = m
while True:
    c = Counter(d)
    mv, mk = max((v,k) for k,v in c.items())
#     print(mv, mk)
    if mv>ans or num==0:
        break
    ans = mv
    used[mk] = False
    num -= 1
    for i in range(n):
        if d[i]==mk:
            for j in range(m):
                if used[a[i][j]]:
                    d[i] = a[i][j]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = list(map(int, input().split()))
a = [None]*n
d = [None]*n
from collections import Counter
for i in range(n):
    a[i] = list(map(lambda x: int(x)-1, input().split()))
    d[i] = a[i][0]

ans = float("inf")
used = [True] * m
while True:
    c = Counter(d)
    mv, mk = max((v,k) for k,v in c.items())
#     print(mv, mk)
    if mv>=ans:
        break
    ans = mv
    used[mk] = False
    for i in range(n):
        if d[i]==mk:
            for j in range(m):
                if used[a[i][j]]:
                    d[i] = a[i][j]
print(ans)

Submission status: Wrong Answer

Solution code:
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():
    # https://atcoder.jp/contests/agc018/tasks/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 i in range(M):
        cnt = defaultdict(int)
        for a in A:
            cnt[a.topleft] += 1

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

    print(res)


if __name__ == "__main__":
    agc018_b()

Submission status: Wrong Answer

Solution code:
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():
    # https://atcoder.jp/contests/agc018/tasks/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()

Submission status: Wrong Answer

Solution code:
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():
    # https://atcoder.jp/contests/agc018/tasks/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()

Submission status: Wrong Answer

Solution code:
import collections

N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
ans = N  # １種類だけ開催
d = dict()
D = collections.defaultdict(list)
for i in range(1, M+1):
    d[i] = 0
for i in range(N):
    for j in range(M):
        d[A[i][j]] += j
        D[A[i][j]].append(j)


d = list(d.items())
d.sort(key=lambda x: x[1], reverse=True)


idx = [M]*N
arr = [0]*M

for i in range(M):
    x = d[i][0]
    for j in range(N):
        #print(idx[j], D[x])
        if idx[j] > D[x][j]:
            arr[x-1] += 1
            if i > 0:
                arr[A[j][idx[j]]-1] -= 1
            idx[j] = D[x][j]
    ans = min(ans, max(arr))
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
ans = M  # １種類だけ開催

s = set()
for _ in range(M):
    count = [0]*M
    for i in range(N):
        for x in A[i]:
            if x in s:
                continue
            else:
                count[x-1] += 1
                break
    ans = min(ans, max(count))
    idx = count.index(max(count))
    s.add(idx + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
import collections

N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
ans = M  # １種類だけ開催
d = dict()
D = collections.defaultdict(list)
for i in range(1, M+1):
    d[i] = 0
for i in range(N):
    for j in range(M):
        d[A[i][j]] += j
        D[A[i][j]].append(j)


d = list(d.items())
d.sort(key=lambda x: x[1], reverse=True)


idx = [M]*N
arr = [0]*M

for i in range(M):
    x = d[i][0]
    for j in range(N):
        #print(idx[j], D[x])
        if idx[j] > D[x][j]:
            arr[x-1] += 1
            if i > 0:
                arr[A[j][idx[j]]-1] -= 1
            idx[j] = D[x][j]
    ans = min(ans, max(arr))
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
    a[i].reverse()

ans = n
used = set()
for i in range(m):
    count = [0]*(m+1)
    for j in range(n):
        count[a[j][-1]] += 1

    ans = min(max(count), ans)
    num = count.index(max(count))
    used.add(num)
    for j in range(n):
        if a[j][-1] in used:
            a[j].pop()

print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
    a[i].reverse()

ans = n
for i in range(m):
    count = [0]*(m+1)
    for j in range(n):
        count[a[j][-1]] += 1

    ans = min(max(count), ans)
    num = count.index(max(count))
    for j in range(n):
        if a[j][-1] == num:
            a[j].pop()

print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
ans = 3000
t = [True] * m
for _ in range(m - 1):
    p = [0] * m
    for i in range(n):
        for x in a[i]:
            if t[x - 1]:
                p[x - 1] += 1
                break
    ma = max(p)
    ans = min(ans, ma)
    t[p.index(ma)] = False
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from operator import itemgetter
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
As = [list(map(int, input().split())) for _ in range(N)]
indices = [0 for i in range(N)] 
#i+1 th person attends his (indices[i]+1) th best favorite sport.

min_turnout = N+1
canceled_sports = set()
for _ in range(M-1):
    popular_sport, turnout = max(Counter(As[i][indices[i]] for i in range(N)).items(), key = itemgetter(1))
    min_turnout = min(min_turnout, turnout)
    canceled_sports.add(popular_sport)
    for i in range(N):
        while As[i][indices[i]] in canceled_sports:
            indices[i] += 1

print(min_turnout)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
table=[list(map(int,input().split())) for _ in range(N)]
buf=[0]*M
cnt=0
ans=float("inf")
for i in range(M,0,-1):
    tosee=[]
    cnt=0
    for j in range(N):
        for k in range(M):
            if table[j][k]==i:
                buf[k]=buf[k]+1
                tosee.append(k)
    tosee=list(set(tosee))
    #print(buf,tosee)
    for j in tosee:
        cnt=max(cnt,buf[j])
    ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
# mod = 10 ** 9 + 7
mod = 998244353 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def f(A, S):
    T = [0] * len(A[0])
    for a in A:
        for k in a:
            if k in S:
                continue
            T[k] += 1
            break
    
    v = max(T)
    return (T.index(v), v)


def main():
    N, M = read_values()
    A = [list(read_index()) for _ in range(N)]

    K = [0] * N

    res = V(min, M)
    S = set()
    for _ in range(M - 1):
        k, v = f(A, S)
        S.add(k)
        res.ud(v)

    print(res)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
# mod = 10 ** 9 + 7
mod = 998244353 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N, M = read_values()
    A = [list(read_index()) for _ in range(N)]

    K = [0] * N
    T = {i: [] for i in range(M)}

    for i in range(N):
        T[A[i][0]].append(i)

    res = V(min, M)
    S = set()
    for _ in range(M - 1):
        k, v = max(T.items(), key=lambda a: len(a[1]))
        res.ud(len(v)) 
        S.add(k)
        for i in v:
            while A[i][K[i]] in S:
                K[i] += 1
            T[A[i][K[i]]].append(i)
        T.pop(k)

    print(res)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, M = getlist()
	D = defaultdict(lambda:1)
	A = []
	for i in range(N):
		a = list(reversed(getlist()))
		A.append(a)

	ans = INF
	for i in range(M - 1):
		cnt = [0] * M
		for j in range(N):
			cnt[A[j][-1] - 1] += 1

		# print(cnt)

		var = max(cnt)
		ans = min(ans, var)
		ind = cnt.index(var)
		D[ind] = 0
		for j in range(N):
			while True:
				if D[A[j][-1] - 1] == 0:
					A[j].pop()
				else:
					break

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 29 22:05:28 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

#N = int(input())
N, M = map(int,input().split())
A = [list(map(int,input().split())) for i in range(N)]


#s = [0 for _ in range(M+1)]
p = [0 for _ in range(N)]
un_held = set([])
ans = M

while len(un_held) < M:
    s = [0 for _ in range(M+1)]
    for i in range(N):
        s[A[i][p[i]]] += 1
    tmp = 0
    for j in range(1,M+1):
        if s[j] > tmp:
            tmp = s[j]
            un = j
    ans = min(tmp,ans)
    un_held.add(un)
    for i in range(N):
        while p[i] < M and A[i][p[i]] in un_held:
            p[i] += 1
    #s[un] = 0


print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [list(reversed(list(map(int, input().split())))) for _ in range(N)]

res = N

while all(A):
    S = [0 for _ in range(M)]
    for a in A:
        S[a[-1] - 1] += 1
    res = min(res, max(S))
    x = S.index(max(S))
    for a in A:
        if a[-1] == x + 1:
            a.pop()

print(res)

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]
for i in range(n):
    for j in range(m):
        A[i][j] -= 1
S = [0] * m
for i in range(n):
    S[A[i][0]] += 1

l = 0
r = n+1
mi = (l+r) // 2
while r-l>1:
    T = S[:]
    ng = 0
    I = [0] * n
    while ng == 0:
        M = [0] * m
        for i in range(m):
            if T[i] > mi:
                M[i] = 1
        # print(T,M)
        if sum(M) == 0:
            break
        for i in range(n):
            if M[A[i][I[i]]] == 1:
                T[A[i][I[i]]] -= 1
                I[i] += 1
                if I[i] == m:
                    ng = 1
                    break
                T[A[i][I[i]]] += 1
    if ng == 1:
        l = mi
    else:
        r = mi
    mi = (l+r) // 2
print(r)