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: Accepted

Solution code:
import collections
N, M = [int(_) for _ in input().split()]
A = [[int(_) for _ in input().split()][::-1] for _ in range(N)]
ng = set()
ans = float('inf')
for m in range(M):
    cnt = collections.defaultdict(int)
    for n in range(N):
        while True:
            if A[n][-1] in ng:
                A[n].pop()
            else:
                cnt[A[n][-1]] += 1
                break
    y = max(cnt, key=lambda x: cnt[x])
    ans = min(ans, cnt[y])
    ng.add(y)
print(ans)
# https://img.atcoder.jp/agc018/editorial.pdf

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

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

from collections import Counter

N,M = II()
A = []
for _ in range(N):
    A.append(III())

used = [False]*(M+1)
place = [0]*N
elements = [0]*N
for i in range(N):
    elements[i] = A[i][0]

ans = float('inf')
for i in range(M):
    c = Counter(elements)
    val = c.most_common(1)[0][0]
    num = c.most_common(1)[0][1]
    if num<ans:
        ans = num
    used[val] = True
    index_list = []
    for j,e in enumerate(elements):
        if e==val:
            index_list.append(j)
    for index in index_list:
        for p in range(place[index],M):
            if used[A[index][p]]:
                continue
            else:
                elements[index] = A[index][p]
                place[index] = p+1
                break

print(ans)

Submission status: Accepted

Solution code:
from collections import deque, defaultdict

n,m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
def check(x):
    """
    最大値をx以下にできるか？
    """
    used = [0]*n
    if x >= n:
        return True
    # NM
    b = [deque(a[i]) for i in range(n)]
    d = defaultdict(list)

    ng = set()
    while True:
        for i in range(n):
            if used[i]:
                continue
            while b[i] and b[i][0] in ng:
                b[i].popleft()
            if not b[i]:
                return False
            like = b[i].popleft()

            d[like].append(i)
            used[i] = 1
        # チェック
        flag = True
        for key, value in d.items():
            if len(value) > x:
                flag = False
                ng.add(key)
                for v in value:
                    used[v] = 0
                d[key] = []
        if flag:
            return True

left = 0
right = n
while right - left > 1:
    mid = (left+right)//2
    if check(mid):
        right = mid
    else:
        left = mid
print(right)

Submission status: Accepted

Solution code:
from collections import deque, defaultdict

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

def check(x):
    used = [0]*n
    if x >= n:
        return True
    # NM
    b = [deque(a[i]) for i in range(n)]
    d = defaultdict(list)

    ng = set()
    while True:
        for i in range(n):
            if used[i]:
                continue
            while b[i] and b[i][0] in ng:
                b[i].popleft()
            if not b[i]:
                return False
            like = b[i].popleft()

            d[like].append(i)
            used[i] = 1
        # チェック
        flag = True
        for key, value in d.items():
            if len(value) > x:
                flag = False
                ng.add(key)
                for v in value:
                    used[v] = 0
                d[key] = []
        if flag:
            return True

left = 0
right = n+1
while right - left > 1:
    mid = (left+right)//2
    if check(mid):
        right = mid
    else:
        left = mid
print(right)

Submission status: Accepted

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


def calc(hold):
    participants = {i: 0 for i in range(1, M + 1)}
    for person in A:
        for sport in person:
            if sport not in hold:
                continue
            participants[sport] += 1
            break
    return participants


X = set(list(range(1, M + 1)))
ans = float('inf')
while X:
    ret_p = calc(X)
    max_participants = max(ret_p.values())
    ans = min(ans, max_participants)
    for key, value in ret_p.items():
        if value == max_participants:
            X.remove(key)
            break

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N,M=map(int,input().split())

L=[]
for i in range(N):
    A=list(map(int,input().split()))
    L.append(A)
    
OUTD=dict()
S=[0 for i in range(N)]

ANS=1000
for i in range(M):
    B=[0 for _ in range(N)]
    for j in range(N):
        for k in range(S[j],M):
            if L[j][k] in OUTD:
                S[j]+=1
            else:
                B[j]=L[j][k]
                break
    X=Counter(B).most_common()[0]
    if X[1]<ANS:
        ANS=X[1]
    OUTD[X[0]]=1
    #print(ANS,B,OUTD)
print(ANS)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC18 B

import sys
import math
import copy
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def f(flag):
    global ans
    cnt = [0]*m
    for i in range(n):
        for j in range(m):
            if flag[a[i][j]-1]:
                cnt[a[i][j]-1] += 1
                break
        else:
            return
    flag[cnt.index(max(cnt))] = False
    ans = min(ans,max(cnt))
    f(flag)
n,m = LI()
a = [LI() for _ in range(n)]
flag = [True]*m
ans = inf
f(flag)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N, M = map(int, input().split())

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

def search(sportList):
    if not sportList:
        return float('inf')

    cnt = defaultdict(int)

    for p in range(N):
        for a in A[p]:
            if a in sportList:
                cnt[a] += 1
                break

    maxSport = -1
    ret = -float('inf')
    for s, c in cnt.items():
        if ret < c:
            maxSport = s
            ret = c

    sportList.remove(maxSport)
    return min(cnt[maxSport], search(sportList))

sportList = set(range(1, M + 1))
ans = search(sportList)

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [[0 for _ in range(M)] for _ in range(N)]
for i in range(N):
  A[i] = [int(x) - 1 for x in input().split()]
S = [[0, i] for i in range(M)]
for a in A:
  S[a[0]][0] += 1
minmax = N
for _ in range(M - 1):
  _S = sorted(S, reverse=True)
  s = _S[0][1]
  minmax = min(S[s][0], minmax)
  S[s][0] = -1
  for a in A:
    if S[a[0]][0] > 0:
      continue
    while S[a[0]][0] < 0:
      a.pop(0)
    S[a[0]][0] += 1
print(minmax)

Submission status: Accepted

Solution code:
import os
import sys
from collections import Counter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
A = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

A = np.array(A, dtype=int)
idx = np.zeros(N, dtype=int)
used = np.zeros(M + 1, dtype=bool)

ans = INF
for _ in range(M):
    k, cnt = Counter(A[np.arange(N), idx]).most_common(1)[0]
    ans = min(ans, cnt)
    used[k] = True
    for i in range(N):
        while idx[i] < M and used[A[i, idx[i]]]:
            idx[i] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N,M=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(N)]
ans = N
ss=set()
front=[0]*N
while len(ss)<M-1:
    cnts = defaultdict(lambda: 0)
    for i,f in enumerate(front):
        cnts[A[i][f]]+=1
    
    maxi, maxc = max(cnts.items(), key=lambda p: p[1])
    ans = min(ans, maxc)
    ss.add(maxi)
    
    for i in range(N):
        while A[i][front[i]] in ss:
            front[i]+=1
print(ans)

Submission status: Accepted

Solution code:
import collections
n,m=map(int,input().split())
A=[]
for i in range(n):
  A.append([int(_) for _ in input().split()])
D=dict()
for i in range(m):
  D[i+1]=1
ans=n
for _ in range(m):
  p=[]
  for i in range(n):
    for j in range(m):
      if D[A[i][j]]==1:
        p.append(A[i][j])
        break
  pp=collections.Counter(p).most_common()
  a,b=pp[0]
  ans=min(ans,b)
  D[a]=0
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

N,M = map(int,input().split())
A = np.array([input().split() for _ in range(N)],dtype=np.int32)

"""
最人気を消去しない → それが採用
消去 → 消去
"""

answer = 10**10
for _ in range(M):
    counter = np.bincount(A[:,0])
    i = counter.argmax()
    max_cnt = counter[i]
    if answer > max_cnt:
        answer = max_cnt
    A = A[A != i].reshape(N,-1)

print(answer)

Submission status: Accepted

Solution code:
from collections import defaultdict
N, M = map(int, input().split())
A = [[] for i in range(N)]
for i in range(N):
    A[i] = list(map(lambda x: int(x) - 1, input().split()))


def calc(k):
    D = [0] * N
    S = [True] * M
    K = M
    while K > 0:
        d = defaultdict(int)
        for i in range(N):
            while D[i] < M and not S[A[i][D[i]]]:
                D[i] += 1
            d[A[i][D[i]]] += 1
        for p, v in d.items():
            if v > k:
                S[p] = False
                K -= 1
                break
        else:
            return True
    return False


l, r = 0, N
while r - l > 1:
    mid = (l + r) // 2
    if calc(mid):
        r = mid
    else:
        l = mid

print(r)

Submission status: Accepted

Solution code:
from collections import Counter

N, M = map(int, input().split())
Ass = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(N)]

def isOK(k):
    iAs = [0] * N
    isAvailables = [True] * M
    numSport = M
    while numSport > 0:
        sports = []
        for i in range(N):
            while iAs[i] < M and not isAvailables[Ass[i][iAs[i]]]:
                iAs[i] += 1
            sports.append(Ass[i][iAs[i]])
        cnt = Counter(sports)
        isOK = True
        for sp, num in cnt.items():
            if num > k:
                isAvailables[sp] = False
                numSport -= 1
                isOK = False
        if isOK:
            return True
    return False

ng, ok = 0, N
while abs(ok-ng) > 1:
    mid = (ng+ok) // 2
    if isOK(mid):
        ok = mid
    else:
        ng = mid

print(ok)

Submission status: Accepted

Solution code:
from collections import Counter, deque

n, m = map(int, input().split())
likes = []
for i in range(n):
    likes.append(deque(map(int, input().split())))

removed = set()
ans = n

for i in range(m):
    count = []
    for qu in likes:
        while qu and qu[0] in removed:
            qu.popleft()
        count.append(qu[0])
    v, c = Counter(count).most_common(1)[0]
    ans = min(ans, c)
    if c == 1:
        break
    removed.add(v)

print(ans)

Submission status: Accepted

Solution code:
def count(A, idx, N):
    cnt = {}
    for i in range(N):
        if A[i][idx[i]] not in cnt:
            cnt[A[i][idx[i]]] = 0
        cnt[A[i][idx[i]]] += 1
    return cnt


def main():
    N, M = map(int, input().split())
    A = [list(input().split()) for _ in range(N)]
    ans = float("inf")
    idx = [0] * N
    used = set()
    for _ in range(M):
        cnt = count(A, idx, N)
        mx = (0, -1)
        for x in cnt:
            mx = max(mx, (cnt[x], x))
        ans = min(ans, mx[0])
        used.add(mx[1])
        for i in range(N):
            while idx[i] < M and A[i][idx[i]] in used:
                idx[i] += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()

def solve():
    N, M = map(int, input().split())
    A = [None] * N
    for i in range(N):
        A[i] = list(map(int, line_input()))
    pointer = [0] * N
    used = set()
    minmax = 500
    for m in range(M):
        num_people = [0] * M
        maxgather = (0, 0)
        for n in range(N):
            num_people[A[n][pointer[n]] - 1] += 1
            if num_people[A[n][pointer[n]] - 1] > maxgather[0]:
                maxgather = (num_people[A[n][pointer[n]] - 1], A[n][pointer[n]])
        used |= {maxgather[1]}
        minmax = min(minmax, maxgather[0])
        for n in range(N):
            while pointer[n] < M:
                if A[n][pointer[n]] in used:
                    pointer[n] += 1
                else: break
    print(minmax)
    return 0
  
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from collections import Counter
from copy import deepcopy
N, M = map(int, input().split())
A = [list(map(int, input().split()))[::-1] for _ in range(N)]
ok = N+1
ng = 0
def check(med, B):
    NG = set()
    for _ in range(N):
        CS = Counter([b[-1] for b in B])
        flag = True
        for k, v in CS.items():
            if v > med:
                NG.add(k)
                flag = False
        if flag:
            return True
        if len(NG) == M:
            return False
        for b in B:
            for _ in range(M):
                if b[-1] not in NG:
                    break
                b.pop()
while abs(ok - ng) > 1:
    B = deepcopy(A)
    med = (ok + ng)//2
    if check(med, B):
        ok = med
    else:
        ng = med

print(ok)

Submission status: Accepted

Solution code:
from collections import defaultdict
from operator import itemgetter
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
ans = float("inf")
for _ in range(M):
    L = []
    cnt = defaultdict(int)
    for a in A:
        if len(a)!=0:
            a0 = a[0]
            L.append(a0)
            cnt[a0] += 1
    ama, ma = max(cnt.items(), key=itemgetter(1))
    ans = min(ans, ma)
    for a in A:
        a.remove(ama)
print(ans)

Submission status: Accepted

Solution code:
import sys, collections
def input():
	return sys.stdin.readline()[:-1]

n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]

def calc(s):#s: 開催しないスポーツ番号のset
	c = collections.Counter()
	for i in range(n):
		for j in range(m):
			if a[i][j] not in s:
				c[a[i][j]] += 1
				break
	tmp = [-1, -1]
	for k, v in c.items():
		if v > tmp[1]:
			tmp = [k, v]
	del c
	return tmp

ans, boycott = n, set()

for _ in range(m-1):
	res = calc(boycott)
	ans = min(ans, res[1])
	boycott.add(res[0])

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,M=map(int,input().split())
A=[[int(i)-1 for i in input().split()] for i in range(N)]
L=set()
#print(A)
def f(L):
    K=[0]*M
    for i in range(N):
        for j in range(M):
            if A[i][j] not in L:
                K[A[i][j]]+=1
                break
    a=0
    b=0
    for i in range(M):
        if a<K[i]:
            a=K[i]
            b=i
    return a,b
ans=10**9
for i in range(M):
    a,b =f(L)
    ans=min(ans,a)
    L.add(b)
    #print(ans,a,b)
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from collections import Counter
n,m=map(int,input().split())
A,B=[],[]
for _ in range(n):
    A.append(tuple(map(lambda x:int(x)-1,input().split())))
    B.append(A[-1][0])
delete=[False]*m
ans=n
idx=[0]*n
for _ in range(m-1):
    c=Counter(B)
    sport,num=c.most_common()[0]
    ans=min(ans,num)
    delete[sport]=True
    for i in range(n):
        while delete[A[i][idx[i]]]:
            idx[i]+=1
        B[i]=A[i][idx[i]]
print(ans)

Submission status: Accepted

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

n,m = map(int,input().split())
a = []
b = []
for i in range(n):
    a.append(tuple(map(lambda x:int(x)-1,input().split())))
    b.append(a[i][0])

used = [False]*m
idx = [0]*n

res = n
for i in range(m-1):
    ctr = Counter(b)
    d = ctr.most_common()
    p,num = d[0]
    res = min(res,num)
    used[p] = True

    for j in range(n):
        while used[a[j][idx[j]]]:
            idx[j] = idx[j] + 1
        b[j] = a[j][idx[j]]

print(res)

Submission status: Accepted

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

ans = N
for i in range(M - 1):
    p = [0] * M
    for j in range(N):
        p[A[j][0] - 1] += 1
    mp = max(p)
    MM = p.index(mp) + 1
    ans = min(ans, mp)
    for j in range(N):
        A[j].remove(MM)

print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
k=[[0]*m for i in range(n)]
for i in range(n):
    k[i]=list(map(int,input().split()))
ans=1000
for i in range(m-1):
    ma=[0]*m
    mi=-1
    for j in range(n):
        ma[k[j][0]-1]+=1
    if ans>max(ma):
        ans=max(ma)
    mi=ma.index(max(ma))+1
    for j in range(n):
        k[j].remove(mi)
if m==1:
    print(n)
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N,M=map(int,input().split())
c=[deque([int(i) for i in input().split()]) for i in range(N)]

G=[0]*M
B=[-1]*N
l=-1
ans=float("inf")
for i in range(M):
  for j in range(N):
    S=len(c[j])
    if B[j]==l:
      for k in range(S):
        s=c[j].popleft()
        if G[s-1]>=0:
          G[s-1]+=1
          B[j]=s-1
          break
  a,l=0,0
  for j in range(M):
    if G[j]>a:
      a,l=G[j],j
  ans=min(ans,a)
  G[l]=-1
print(ans)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#

"""
AGC018 B
"""

from collections import Counter

n, m = map(int, input().split())
alili = [list(map(int, input().split())) for i in range(n)]

cnt = Counter()

for i in range(n):
    cnt[alili[i][0]] += 1

ans = cnt.most_common(1)[0][1]
delset = set()

for j in range(m-1):
    x, tmp = cnt.most_common(1)[0]
    if tmp < ans:
        ans = tmp
    del cnt[x]
    delset.add(x)
    for i in range(n):
        if alili[i][0] == x:
            while alili[i][0] in delset:
                del alili[i][0]
            cnt[alili[i][0]] += 1

print(ans)

Submission status: Accepted

Solution code:
n, m=map(int, raw_input().split())
l=[map(int, raw_input().split()) for i in range(n)]
last, first=0, n
while last+1!=first:
	mid=last+(first-last)/2
	erased=[False]*(m+1)
	bucket=[[] for i in range(m+1)]
	for i in range(n):
		bucket[l[i][0]].append((i, 0))
	ok=True
	while True:
		any_change=False
		for i in range(m+1):
			if len(bucket[i])>mid:
				erased[i]=True
				for which, index in bucket[i]:
					while index<m and erased[l[which][index]]:
						index+=1
					if index==m:
						ok=False
						break
					bucket[l[which][index]].append((which, index))
				bucket[i]=[]
				any_change=True
		if not any_change or not ok:
			break
	if ok:
		first=mid
	else:
		last=mid
print first

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ans = 10 ** 9
lis = [list(map(int,input().split())) for i in range(n)]
for i in range(m):
  num = [0 for i in range(m)]
  for j in range(n):num[lis[j][0]-1] += 1
  ans = min(max(num),ans)
  for k in range(n):lis[k].remove(num.index(max(num))+1)
print(ans)

Submission status: Accepted

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

ans = float('inf')
sport_num = M
adopt_sports = [1] * M
for it in [0]*M:
    participate_sports = [0] * M
    for n in range(N):
        for m in range(M):
            if adopt_sports[A[n][m]-1] == 1:
                participate_sports[A[n][m]-1] += 1
                break

    max_v = 0
    max_idx = -1
    for m in range(M):
        if participate_sports[m] > max_v:
            max_idx = m
            max_v = participate_sports[m]

    ans = min(max_v,ans)
    adopt_sports[max_idx] = 0

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, M = map( int, input().split())
A = [ [] for _ in range(N)]
for i in range(N):
    A[i] = list( map( int, input().split())) 
V = [1]*(M+1)
ans = N
for _ in range(M-1):
    W = []
    for i in range(N):
        while True:
            if V[A[i][0]] == 1:
                W.append(A[i][0])
                break
            else:
                A[i].pop(0)
    a, c = Counter(W).most_common()[0]
    ans = min( ans, c)
    V[a] = 0
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

def sub(prefers, canceled):
    res = Counter()
    for pref in prefers:
        p = pref[-1]
        while p in canceled:
            pref.pop()
            p = pref[-1]
        res[p] += 1
    return max(res.items(), key=itemgetter(1))

def solve(m, prefers):
    canceled = set()
    ans = float('inf')
    for _ in [0] * m:
        s, c = sub(prefers, canceled)
        canceled.add(s)
        ans = min(ans, c)
    return ans

if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [list(map(int, input().split()))[::-1] for _ in [0] * N]
    print(solve(M, A))

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

def sub(prefers, canceled):
    res = Counter()
    for pref in prefers:
        pt = -1
        while pref[pt] in canceled:
            pt -= 1
        res[pref[pt]] += 1
    return max(res.items(), key=itemgetter(1))

def solve(m, prefers):
    canceled = set()
    ans = float('inf')
    for _ in [0] * m:
        s, c = sub(prefers, canceled)
        canceled.add(s)
        ans = min(ans, c)
    return ans

if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [list(map(int, input().split()))[::-1] for _ in [0] * N]
    print(solve(M, A))

Submission status: Accepted

Solution code:
n,m = (int(i) for i in input().split())
a = [[int(i) for i in input().split()] for i in range(n)]
b,x,num = [0]*n,[0]*(m+1),m
for i in range(n): x[a[i][0]]+=1
z = max(x)
ans,x[x.index(z)] = z,-1
while ans>(n-1)//num+1:
	for i in range(n):
		if x[a[i][b[i]]]==-1:
			while x[a[i][b[i]]]==-1: b[i]+=1
			x[a[i][b[i]]]+=1
	z = max(x)
	ans,x[x.index(z)],num = min(ans,z),-1,num-1
print(ans)

Submission status: Accepted

Solution code:

def check(mask):
    for e in mask:
        if e==1: return True
    return False

def main():
    n,m=map(int,input().split(" "))
    ia=[list(map(int,input().split(" "))) for _ in range(n)]
    a=[[0 for _ in range(m)] for _ in range(n)]
    for i in range(n):
        for j in range(m):
            a[i][ia[i][j]-1]=j
    mask=[1 for _ in range(m)]
    res=1000
    while check(mask):
        cnt=[0 for _ in range(m)]
        for alst in a:
            mi=3000
            pos=-1
            for i in range(m):
                if alst[i]<mi and mask[i]==1:
                    pos=i
                    mi=alst[i]
            cnt[pos]+=1
        ma=max(cnt)
        res=min(res,ma)
        for i in range(m):
            if cnt[i]==ma: mask[i]=0

    print(res)            


main()

Submission status: Accepted

Solution code:
import collections
from collections import deque
n,m = map(int,input().split())
ko = deque([])
for i in range(n):
  a= deque(map(int,input().split()))
  ko.append(a)
dai = float("inf")
for j in range(m):
  san = []
  for i in range(n):
    san.append(ko[i][0])
  c = collections.Counter(san)
  to = c.most_common()[0]
  dai = min(to[1],dai)
  ira = to[0]
  for i in range(n):
    ko[i].remove(ira)
print(dai)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys, math, copy
# import fractions, itertools
# import numpy as np
# import scipy

HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def each_attend(anm, held):
    n = len(anm)
    m = len(anm[0])
    atte = []
    for i in range(n):
        aim = anm[i]
        for k in aim:
            if k in held:
                break
        atte.append(k)
    return atte

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

    held = set(range(1, m + 1))
    res = HUGE
    while len(held) > 0:
        atte = each_attend(anm, held)
        num_atte = {k: 0 for k in range(1, m + 1)}
        for k in atte:
            num_atte[k] += 1
        ma = max(num_atte.values())
        res = min(res, ma)
        popest_sports = set(filter(lambda k: num_atte[k] == ma, held))
        held -= popest_sports
    print(res)

main()

Submission status: Accepted

Solution code:
from collections import Counter

N,M = map(int,input().split())
src = [list(map(lambda x:int(x)-1,input().split())) for i in range(N)]

removed = [0] * M
idxes = [0] * N
ans = N
for _ in range(M-1):
    ctr = Counter([src[i][idxes[i]] for i in range(N)])
    k,most = ctr.most_common(1)[0]
    ans = min(ans, most)
    removed[k] = 1
    for i in range(N):
        while removed[src[i][idxes[i]]]:
            idxes[i] += 1
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[]
for i in range(N):
  a=[int(x) for x in input().split()]
  A.append(a)
  
def solve():
  res=[ 0 for x in range(M+1)]
  for i in range(N):
    res[A[i][0]]+=1
  ans=max(res)
  jud=res.index(ans)
  for j in range(N):
    A[j].remove(jud)
  return ans

minans=N
for i in range(M):
  minans=min(solve(),minans)
print(minans)

Submission status: Accepted

Solution code:
import sys
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
itr=[0 for i in range(n)]
opend=[True for i in range(m)]
back=[[] for i in range(m)]
for i in range(n):
    back[a[i][0]].append(i)
ret=10**9
while True:
    tmp=0
    for i in range(m):
        tmp=max(tmp,len(back[i]))
    ret=min(ret,tmp)
    for i in range(m):
        if len(back[i])>=ret:
            opend[i]=False
            for j in range(len(back[i])):
                source=back[i][j]
                while not opend[a[source][itr[source]]]:
                    itr[source]+=1
                    if itr[source]==m:
                        print(ret)
                        sys.exit()
                back[a[source][itr[source]]].append(source)
            back[i]=[]

Submission status: Accepted

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

S_list = {}
for i in range(M):
    S_list[i+1] = 0

participants = 10000
while len(S_list) > 0:
    for i in range(N):
        for j in range(M):
            if A[i][j] in S_list:
                S_list[A[i][j]] += 1
                break
    participants = min(participants,max(S_list.values()))
    if participants == 1:
        break
    for i in S_list.keys():
        if S_list[i] == max(S_list.values()):
            del S_list[i]
            break
    for i in S_list.keys():
        S_list[i] = 0

print(participants)

Submission status: Accepted

Solution code:
from collections import defaultdict
from operator import itemgetter


def sub(prefers, canceled):
    res = defaultdict(int)
    for pref in prefers:
        p = pref[-1]
        while p in canceled:
            pref.pop()
            p = pref[-1]
        res[p] += 1

    return max(res.items(), key=itemgetter(1))


def solve(m, prefers):
    canceled = set()
    ans = float('inf')
    for _ in range(m):
        s, c = sub(prefers, canceled)
        canceled.add(s)
        ans = min(ans, c)
    return ans


n, m = map(int, input().split())
prefers = [list(reversed(list(map(int, input().split())))) for _ in range(n)]

print(solve(m, prefers))

Submission status: Accepted

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 = A[i]
    b = [0]*M
    for j in range(M):
        b[a[j]-1] = M-j
    A[i] = b

select = [set() for i in range(M)]
cur = [[1]*(M+1) for i in range(N)]
mp = [[M+1]*(M+1) for i in range(N)]
I = [M]*N
for i in range(N):
    mpi = mp[i]
    Ai = A[i]
    for j in range(M):
        mpi[Ai[j]] = j
    select[mpi[M]].add(i)

ans = N
cnt = 0
for t in range(M+1):
    ma = max(map(len, select))
    ans = min(ans, ma)
    rm = []
    for i in range(M):
        if len(select[i]) == ma:
            rm.append(i)
    target = set()
    for i in rm:
        for j in range(N):
            cur[j][A[j][i]] = 0
        target |= select[i]
        select[i] = []
        cnt += 1
    if cnt == M:
        break
    for i in target:
        p = I[i]
        c = cur[i]
        while c[p] == 0:
            p -= 1
        I[i] = p
        select[mp[i][p]].add(i)
print(ans)