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:
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 = m+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)

Submission status: Wrong Answer

Solution code:
#解説参照
#全部やる状態から最大参加者のスポーツを消す
import sys
input = sys.stdin.readline
n,m = map(int, input().split( ))
a = [list(map(int, input().split( ))) for _ in range(n)]

M = {i for i in range(1,m+1)}
cnt = [0]*(m+1)
cursor = [0]*n
for i in range(n):
    cnt[a[i][0]]+=1

q = max(cnt)
ans = q
p = cnt.index(q)
for _ in range(q):
    cnt[p]=0
    M.remove(p)
    for i in range(n):
        if a[i][cursor[i]]==p:
            while a[i][cursor[i]] not in M:
                cursor[i]+=1
                if cursor[i]==m:
                    print(ans)
                    exit()
            cnt[a[i][cursor[i]]]+=1
    q2 = max(cnt)
    ans = min(ans,q2)
    p = cnt.index(q2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N)]
def C(x):
    count = [0]*M
    index = [0]*N
    flag = [False]*M
    for i in range(N):
        count[A[i][0]]+=1
    m = max(count)
    while m>x:
        tmp = [0]*M
        for i in range(N):
            j = index[i]
            if count[A[i][j]]>x:
                flag[A[i][j]] = True
                while flag[A[i][j]]:
                    index[i]+=1
                    j+=1
                    if j == M:
                        return False
            tmp[A[i][j]]+=1
        count = [t for t in tmp]
        m = max(count)
    return True

ng = 0
ok = N
while ok-ng>1:
    mid = (ok+ng)//2
    if C(mid):ok = mid
    else:ng = mid
print(ok)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
t = [0] * (M + 1)
p = [0] * N
s = set()
res = float("inf")
for _ in range(M):
  t = [0] * (M + 1)
  for i in range(N): t[a[i][p[i]]] += 1
  x = 0
  y = 0
  for j in range(1, M + 1):
    if y < t[j]:
      x = j
      y = t[j]
  s.add(x)
  for i in range(N):
    while a[i][p[i]] in s:
      p[i] += 1
      if p[i] >= M:
        break
  if max(p) >= M: break
  res = min(res, max(t))
  #print(t, p)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
t = [0] * (M + 1)
p = [0] * N
res = float("inf")
for _ in range(M):
  t = [0] * (M + 1)
  for i in range(N): t[a[i][p[i]]] += 1
  x = 0
  y = 0
  for j in range(1, M + 1):
    if y < t[j]:
      x = j
      y = t[j]
  for i in range(N):
    if a[i][p[i]] == x:
      p[i] += 1
  res = min(res, max(t))
print(res)

Submission status: Wrong Answer

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(x):
    b = []
    for i in range(n):
        b.append([y for y in a[i]])
    #ddprint(b)
    cnt = 0
    rem = [False]*m
    while cnt<m:
        #ddprint("in loop x {} cnt {} b, rem".format(x,cnt))
        #ddprint(b)
        #ddprint(rem)
        top = [0]*m
        ok = True
        for i in range(n):
            top[b[i][-1]] += 1
        for j in range(m):
            if top[j]>x:
                ok = False
                rem[j] = True
                cnt += 1
                if cnt==n:
                    return False
        if ok:
            return True
        for i in range(n):
            while len(b[i])>0 and rem[b[i][-1]]:
                b[i].pop()
    return True

n,m = inm()
a = []
for i in range(n):
    aa = inl()
    aa = [x-1 for x in aa]
    aa.reverse()
    a.append(aa)

ddprint(a)
mx = n
mn = 0
while mx>mn+1:
    mid = (mx+mn)//2
    if foo(mid):
        mx = mid
    else:
        mn = mid
print(mx)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
dp = [N * M] * M
t = [M] * N
vis = set()
for i in range(M):
  x = 0
  y = 0
  for j in range(M):
    z = 0
    for k in range(N):
      z += a[k][j] == t[k]
    if z > y:
      y = z
      x = j
  dp[i] = y
  vis.add(x)
  for k in range(N):
    w = 0
    for j in range(M):
      if j in vis: continue
      w = max(w, a[k][j])
    t[k] = w
print(min(dp))

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
n,m=map(int,input().split())
A=[[int(i) for i in input().split()] for _ in range(n)]
l=[int(i)+1 for i in range(m)]
min_=n
while True:
    d=defaultdict(int)
    for j in range(n):
        c=A[j-1]
        for i in range(m):
            if c[i] in l:
                d[c[i]] += 1
                break
    s=sorted(list(zip(d.keys(),d.values())),key = lambda x: x[1])
    
    if len(s) !=0 :
        if s[-1][1] <= min_:
            min_=s[-1][1]
            l.remove(s[-1][0])
        else:
            print(min_)
            sys.exit()
    else:
        print(min_)
        sys.exit()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def update_rank_lis(rank_lis, drop_sport):
    return [
        [sport for sport in ranks if sport != drop_sport]
        for ranks in rank_lis
    ]

def count_first(rank_lis):
    first_cnt = Counter([ranks[0] for ranks in rank_lis])
    sorted_cnt_sports = sorted([(cnt, sport) for sport, cnt in first_cnt.items()])[::-1]
    max_cnt = sorted_cnt_sports[0][0]
    return ((cnt, sport) for cnt, sport in sorted_cnt_sports if cnt == max_cnt)


def search(rank_lis,  min_value, depth):
    for _ in range(depth):
        candidate_cnt_spots = count_first(rank_lis)
        for most_pop_cnt, most_pop_sport in count_first(rank_lis):
            min_value = min(most_pop_cnt, min_value)
            updated_rank_lis = update_rank_lis(rank_lis, most_pop_sport)
            searched_value = search(updated_rank_lis, min_value, depth - 1)
            min_value = min(min_value, searched_value)
    return min_value


def solve(N, M, rank_lis):
    min_value = search(rank_lis, M, M)
    print(min_value)
    
N, M = map(int, input().split())
rank_lis = [list(map(int, input().split())) for _ in range(N)]
solve(N, M, rank_lis)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def update_rank_lis(rank_lis, drop_sport):
    return [
        [sport for sport in ranks if sport != drop_sport]
        for ranks in rank_lis
    ]

def count_first(rank_lis):
    first_cnt = Counter([ranks[0] for ranks in rank_lis])
    return sorted([(cnt, sport) for sport, cnt in first_cnt.items()])[-1]

def solve(N, M, rank_lis):
    min_value = M
    for _ in range(M):
        most_pop_cnt, most_pop_sport = count_first(rank_lis)
        min_value = min(most_pop_cnt, min_value)
        rank_lis = update_rank_lis(rank_lis, most_pop_sport)
    print(min_value)
    
N, M = map(int, input().split())
rank_lis = [list(map(int, input().split())) for _ in range(N)]
solve(N, M, rank_lis)

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())

# 0~m-1のスポーツに変換
a = []
for i in range(n):
    b = list(map(int, input().split()))
    for j in range(len(b)):
        b[j]-=1
    a.append(b)

# i番目のスポーツを実施するなら1
held_sports = [1 for i in range(m)]

# i番目の人は何番目に好きなスポーツやるか？
join_sports = [0 for i in range(n)]

ans = n
for k in range(m):
    # i番目のスポーツへの参加人数
    total_join_sports = [0 for i in range(m)]
    for i in range(n):
        total_join_sports[a[i][join_sports[i]]]+=1

    # [最大スポーツ, 参加人数]
    sports = [0,0]
    for i in range(m):
        if total_join_sports[i]>sports[1]:
            sports = [i,total_join_sports[i]]

    # 現時点での最小出す
    ans = min(ans, sports[1])

    # 最大スポーツやめる
    held_sports[sports[0]]=0

    # 参加し直し
    for i in range(n):
        # i番目の人はjoin_sports[i](x)番目に好きなスポーツに参加していた
        x = join_sports[i]
        if a[i][x]==sports[0]:
            # x番目以降の好きなスポーツ、かつ、やっているスポーツ探す
            for j in range(x+1,m):
                if held_sports[a[i][j]]==1:
                    join_sports[i]=j
print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())

a = []
for i in range(n):
    b = list(map(int, input().split()))
    for j in range(len(b)):
        b[j]-=1
    a.append(b)

# 実施するなら1
held_sports = [1 for i in range(m)]

# 何番目に好きなスポーツやるか？
join_sports = [0 for i in range(n)]

ans = float('inf')
for k in range(m):
    # 各スポーツへの参加人数
    total_join_sports = [0 for i in range(m)]
    for i in range(n):
        total_join_sports[a[i][join_sports[i]]]+=1

    # [最大スポーツ, 参加人数]
    sports = [0,0]
    for i in range(m):
        if total_join_sports[i]>sports[1]:
            sports = [i,total_join_sports[i]]

    # 現時点での最小出す
    ans = min(ans, sports[1])

    # 最大スポーツやめる
    held_sports[sports[0]]=0

    # 参加し直し
    for i in range(n):
        if held_sports[a[i][join_sports[i]]]==0:
            for j in range(join_sports[i]+1,m):
                if held_sports[a[i][j]]!=0:
                    join_sports[i]=j
print(ans)

Submission status: Wrong Answer

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

def func(p):
    if sum(p) == 0:
        return M
    count = [0]*M
    for l in A:
        i = 0
        while p[l[i]-1] == 0:
            i += 1
        count[l[i]-1] += 1
    hoge = max(count)
    ret = hoge
    for i in range(M):
        if count[i] == hoge:
            q = p[:]
            q[i] = 0
            ret = min(func(q),ret)
    return ret

print(func([1]*M))

Submission status: Wrong Answer

Solution code:
import collections
import fractions
N,M=map(int,input().split())
A=[list(map(int,input().split())) for i in range(N)]
"""suki=[[0,i] for i in range(M)]
for n in range(N)
    for m in range(M):
        suki[A[n][m]-1]+=(300-n)
suki=sorted(suki,key=lambda x:x[0],reverse=True)
value=max([i[0] for i in A])
lst{}

for s in suki:
    lst.add(s)
    for n in range(N):
        if
        for m in range(M):
"""

lst=set()
value=M
for k in range(M):
    data=[0 for m in range(0,M+1)]
    for n in range(N):
        for m in range(M):
            if A[n][m] not in lst:
                data[A[n][m]]+=1
                break
    #print(lst)
    value=min(max(data),value)
    lst.add(data.index(max(data)))
print(value)

Submission status: Wrong Answer

Solution code:
from numpy import*
a=loadtxt(open(0),'i',skiprows=1,ndmin=2)
n,m=a.shape
while a.any():
  t=bincount(a[:,0])
  m=min(m,max(t))
  a=reshape(a[a!=argmax(t)],(n,-1))
print(m)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = N
sports_cand = set([v for v in range(M)])
for _ in range(M-2):
    C = Counter()
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                C[mat[n][m]] += 1
                break
    tmp = 0
    key, val = C.most_common(1)[0]
    ans = min(ans, val)
    sports_cand -= {key}
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = N
Q = [set([v for v in range(M)])]
while Q:
    C = Counter()
    sports_cand = Q.pop(0)
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                C[mat[n][m]] += 1
                break
    tmp = 0
    for key, val in C.most_common():
        if val >= tmp:
            ans = min(ans, val)
            if len(sports_cand) > 2:
                Q.append(sports_cand-{key})
        else:
            break
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = N
Q = [set([v for v in range(M)])]
while Q:
    sports_cand = Q.pop(0)
    attend_sports = [-1]*N
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                attend_sports[n] = mat[n][m]
                break
    C = Counter(attend_sports)
    num = 0
    for id, tmp_val in C.most_common():
        if tmp_val >= num:
            num = tmp_val
            ans = min(ans, tmp_val)
            if len(sports_cand) > 2:
                Q.append(sports_cand-{id})
        else:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = N
Q = [set([v for v in range(N)])]
while Q:
    sports_cand = Q.pop(0)
    attend_sports = [-1]*N
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                attend_sports[n] = mat[n][m]
                break
    C = Counter(attend_sports)
    num = 0
    for id, tmp_val in C.most_common():
        if tmp_val >= num:
            num = tmp_val
            ans = min(ans, tmp_val)
            if len(sports_cand) > 2:
                Q.append(sports_cand-{id})
        else:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = N
Q = [set([v for v in range(N)])]
while Q:
    sports_cand = Q.pop(0)
    attend_sports = [-1]*N
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                attend_sports[n] = mat[n][m]
                break
    C = Counter(attend_sports)
    num = 0
    for id, tmp_val in C.most_common():
        if tmp_val >= num:
            num = tmp_val
            ans = min(ans, tmp_val)
            if len(sports_cand) <= 2:
                break
            Q.append(sports_cand-{id})
        else:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

sports_cand = set([v for v in range(M)])

ans = N
Q = [sports_cand]
while Q:
    sports_cand = Q.pop(0)
    attend_sports = [-1]*N

    for p in range(N):
        for m in range(M):
            if mat[p][m] in sports_cand:
                attend_sports[p] = mat[p][m]
                break
    C = Counter(attend_sports)
    num = 0
    for id, tmp_val in C.most_common():
        if tmp_val >= num:
            num = tmp_val
            ans = min(ans, tmp_val)
            if len(sports_cand) <= 2:
                break
            Q.append(sports_cand-{id})
        else:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = float('inf')
sports_cand = set([v for v in range(M)])


def rec(sports_cand, ans):
    if not sports_cand:
        return ans
    else:
        attend_i = [0]*N  # iの参加したいポイント
        attend_sports = [-1]*N  # iの参加するスポーツ
        for s in sports_cand:
            for p in range(N):
                if attend_i[p] < mat[p][s]:  # 今までに見たスポーツよりも参加したい
                    attend_i[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        print("c->", C)
        num = 0
        for id, tmp_val in C.most_common():
            if tmp_val >= num:
                num = tmp_val
                ans = min(ans, tmp_val)
                if len(sports_cand) <= 2:
                    break
                ans = min(ans, rec(sports_cand-{id}, ans))
            else:
                break
    return ans


print(rec(sports_cand, ans))

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = float('inf')
sports_cand = set([v for v in range(M)])


def rec(sports_cand, ans):
    if not sports_cand:
        return ans
    else:
        attend_i = [0]*N  # iの参加したいポイント
        attend_sports = [-1]*N  # iの参加するスポーツ
        for s in sports_cand:
            for p in range(N):
                if attend_i[p] < mat[p][s]:  # 今までに見たスポーツよりも参加したい
                    attend_i[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        num = 0
        for id, tmp_val in C.most_common():
            if tmp_val >= num:
                num = tmp_val
                ans = min(ans, tmp_val, rec(sports_cand-{id}, ans))
            else:
                break
    return ans


print(rec(sports_cand, ans))

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = float('inf')
sports_cand = set([v for v in range(M)])


def rec(sports_cand, ans):
    if not sports_cand:
        return ans
    else:
        attend_i = [0]*N  # iの参加したいポイント
        attend_sports = [-1]*N  # iの参加するスポーツ
        for s in sports_cand:
            for p in range(N):
                if attend_i[p] < mat[p][s]:  # 今までに見たスポーツよりも参加したい
                    attend_i[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        num = 0
        for id, tmp_val in C.most_common():
            if tmp_val >= num and id == -1:
                continue
            elif tmp_val >= num:
                num = tmp_val
                ans = min(ans, tmp_val)
                ans = min(ans, rec(sports_cand-{id}, ans))
            else:
                break
    return ans


print(rec(sports_cand, ans))

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = float('inf')
sports_cand = set([v for v in range(M)])


def rec(sports_cand, ans):
    if not sports_cand:
        return ans
    else:
        attend_i = [0]*N  # iの参加したいポイント
        attend_sports = [-1]*N  # iの参加するスポーツ
        for s in sports_cand:
            for p in range(N):
                if attend_i[p] < mat[p][s]:  # 今までに見たスポーツよりも参加したい
                    attend_i[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        num = 0
        for id, tmp_val in C.most_common():
            if tmp_val >= num:
                num = tmp_val
                if id != -1:
                    ans = min(ans, tmp_val)
                    ans = min(ans, rec(sports_cand-{id}, ans))
            else:
                break
    return ans


print(rec(sports_cand, ans))

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

ans = float('inf')
sports_cand = set([v for v in range(M)])


def rec(sports_cand, ans):
    if not sports_cand:
        return ans
    else:
        attend_i = [0]*N  # iの参加したいポイント
        attend_sports = [-1]*N  # iの参加するスポーツ
        for s in sports_cand:
            for p in range(N):
                if attend_i[p] < mat[p][s]:  # 今までに見たスポーツよりも参加したい
                    attend_i[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        num = 0
        for id, tmp_val in C.most_common():
            if tmp_val >= num:
                num = tmp_val
                ans = min(ans, tmp_val)
                ans = min(ans, rec(sports_cand-{id}, ans))
            else:
                break
    return ans


print(rec(sports_cand, ans))

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    N, M = map(int, input().split())
    mat = [list(map(int, input().split())) for _ in range(N)]

    ans = float('inf')
    sports_cand = set([v for v in range(M)])
    while sports_cand:
        attend_i = [0]*N  # iの参加したいポイント
        attend_sports = [-1]*N  # iの参加するスポーツ
        for s in sports_cand:
            for p in range(N):
                if attend_i[p] < mat[p][s]:  # 今までに見たスポーツよりも参加したい
                    attend_i[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        top_sports_id, num = C.most_common(1)[0]
        ans = min(ans, num)
        sports_cand.remove(top_sports_id)
    print(ans)


solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    N, M = map(int, input().split())
    mat = [list(map(int, input().split())) for _ in range(N)]

    ans = 300
    sports_cand = set([v for v in range(M)])
    while sports_cand:
        attend_n = [0]*N
        attend_sports = [-1]*N
        for s in sports_cand:
            for p in range(N):
                if attend_n[p] < mat[p][s]:
                    attend_n[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        top_sports_id, num = C.most_common(1)[0]
        ans = min(ans, num)
        sports_cand.remove(top_sports_id)
    print(ans)


solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))
from collections import defaultdict


def main():
    n, m = map(int, input().split())
    a = []
    for _ in range(n):
        a.append(list(map(int, input().split())))
    sports = [1 for _ in range(m)]
    ans = 10**18
    for _ in range(m-1):
        sdic = defaultdict(lambda: 0)
        for _al in a:
            for _a in _al:
                if sports[_a-1] == 1:
                    sdic[_a] += 1
                    break
        tmp = list(sorted(sdic.items(), key=lambda x: x[1]))[-1]
        ans = min(ans, tmp[1])
        sports[tmp[0]-1] = 0
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[list(map(int,input().split())) for i in range(N)]
g=[1]*M
G=M
I=[0]*N
R=M
def count(x):
    c=[0]*M
    r,ri=0,0
    for e in x:
        c[e-1]+=1
        if c[e-1]>r:
            r=c[e-1]
            ri=e-1
    return r,ri
while G>0:
    for l,a in enumerate(A):
        while g[a[I[l]]-1]==0:
            I[l]+=1
    r,ri=count([a[I[l]] for l,a in enumerate(A)])
    R=min(R,r)
    g[ri]-=1
    G-=1
print(R)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[list(map(int,input().split())) for i in range(N)]
g=[1]*M
G=M
I=[0]*N
R=M
def count(x):
    c=[0]*M
    r,ri=0,0
    for e in x:
        c[e-1]+=1
        if c[e-1]>r:
            r=c[e-1]
            ri=e-1
    return r,ri
while G>1:
    for l,a in enumerate(A):
        while g[a[I[l]]-1]==0:
            I[l]+=1
    r,ri=count([a[I[l]] for l,a in enumerate(A)])
    R=min(R,r)
    g[ri]-=1
    G-=1
print(R)

Submission status: Wrong Answer

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
    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
    used[val] = True

print(ans)

Submission status: Wrong Answer

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].pop()
        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: Wrong Answer

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

def main():
    N,M = map(int,input().split())
    A = [list(map(int,input().split())) for _ in range(N)]
    play = [(i+1) for i in range(M)]
    ans = M
    for k in range(M):
        do = [0 for _ in range(M)]
        for i in range(N):
            for j in range(M):
                if A[i][j] in play:
                    do[A[i][j]-1] += 1
                    break
        l = do.index(max(do))+1
        ans = min(ans,max(do))
        play.remove(l)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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:
    while S[a[0]][0] < 0:
      a.pop(0)
    S[a[0]][0] += 1
print(minmax)

Submission status: Wrong Answer

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 a[0] == s:
      a.pop(0)
      S[a[0]][0] += 1
print(minmax)

Submission status: Wrong Answer

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(n-1):
	res = calc(boycott)
	#print(res)
	ans = min(ans, res[1])
	boycott.add(res[0])

print(ans)

Submission status: Wrong Answer

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, 0]
	for k, v in c.items():
		if v > tmp[1]:
			tmp = [k, v]
	del c
	return tmp

ans, boycott = n, set()

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

print(ans)