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:
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]
	return tmp

ans, boycott = n, set()

for _ in range(n):
	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]
	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
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: Wrong Answer

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(N):
    a,b =f(L)
    ans=min(ans,a)
    L.add(b)
    #print(ans,a,b)
print(ans)

Submission status: Wrong Answer

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 = m
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: Wrong Answer

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 = 10**9
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: Wrong Answer

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)
print(ans)

Submission status: Wrong Answer

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]

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

print(ans)

Submission status: Wrong Answer

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 A[i]:
            a = A[i].pop(0)
            if V[a] == 1:
                W.append(a)
                break
    if not W:
        break
    a, c = Counter(W).most_common()[0]
    ans = min( ans, c)
    V[a] = 0
print(ans)

Submission status: Wrong Answer

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):
        a = A[i].pop(0)
        if V[a] == 1:
            W.append(a)
    if not W:
        break
    a, c = Counter(W).most_common()[0]
    ans = min( ans, c)
    V[a] = 0
print(ans)

Submission status: Wrong Answer

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 A[i]:
            a = A[i].pop(0)
            if V[a] == 1:
                W.append(a)
    if not W:
        break
    a, c = Counter(W).most_common()[0]
    ans = min( ans, c)
    V[a] = 0
print(ans)

Submission status: Wrong Answer

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):
    if ko[i][0] == ira:
      ko[i].popleft()
print(dai)

Submission status: Wrong Answer

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 i < M and removed[src[i][idxes[i]]]:
            idxes[i] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from collections import Counter

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

qs = [deque(row) for row in src]
ctr = Counter([q[0] for q in qs])

ans = N
for i in range(M):
    k,v = ctr.most_common(1)[0]
    ans = min(ans, v)
    del ctr[k]
    for q in qs:
        if not q or q[0] != k: continue
        q.popleft()
        if q:
            ctr[q[0]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
a = [None] * n
for i in range(n): a[i] = [int(x) for x in input().split()]
def s(i, x):
    if i == m:
        if len(x) == 0: return 301
        d = [0] * m
        for j in range(n):
            M = a[j][x[0]]
            l = x[0]
            for k in x:
                if a[j][k] > M:
                    M = a[j][k]
                    l = k
            d[l] += 1
        #print(d)
        return max(d)
    x.append(i)
    r = s(i + 1, x)
    x.pop()
    return min(r, s(i + 1, x))
print(s(0, []))

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

Submission status: Wrong Answer

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

can = [1]*M
ans = N
while sum(can):
    select = [0]*M
    for i in range(N):
        idx = A[i].index(max(A[i][j] for j in range(M) if can[j]))
        select[idx] += 1

    ans = min(ans, max(select))
    m_idx = select.index(max(select))
    can[m_idx] = 0
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)]

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))
    assert sum(map(len, select)) == N
    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)

Submission status: Wrong Answer

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

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):
    idx = A[i].index(M)
    select[idx].add(i)
    mpi = mp[i]
    Ai = A[i]
    for j in range(M):
        mpi[Ai[j]] = j

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 range(N):
    #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)

Submission status: Wrong Answer

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

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):
    idx = A[i].index(M)
    select[idx].add(i)
    mpi = mp[i]
    Ai = A[i]
    for j in range(M):
        mpi[Ai[j]] = j

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)

Submission status: Wrong Answer

Solution code:
people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) - 1 for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
for _ in range(sports - 1):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
    answer = min(answer, max(rank))
    use_sports -= {rank.index(max(rank))}
print(answer)

Submission status: Wrong Answer

Solution code:
def dfs(people, sports, use_sports, answer):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
            will_delete = []
    max_p = max(rank)
    exclude = rank.index(max_p)
    if len(use_sports) > 1:
        answer = min(answer, max_p, dfs(people, sports, use_sports - {exclude}, answer))
    return answer

people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
print(dfs(people, sports, use_sports, answer))

Submission status: Wrong Answer

Solution code:
def dfs(people, sports, use_sports, answer):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
            will_delete = []
    max_p = max(rank)
    for ra,nu in enumerate(rank):
        if nu == max_p and len(use_sports) > 1:
            answer = min(answer, max_p, dfs(people, sports, use_sports - {ra}, answer))
    return answer

people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
print(dfs(people, sports, use_sports, answer))

Submission status: Wrong Answer

Solution code:
people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
for _ in range(sports - 1):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
    answer = min(answer, max(rank))
    use_sports -= {rank.index(max(rank))}
print(answer)

Submission status: Wrong Answer

Solution code:

import math

# 配列の中で一番多い要素を返す
def maxElem( lis ):
  
  L = lis[:]#copy
  S = set(lis)
  S = list(S)
  MaxCount=0
  ret='nothing...'

  for elem in S:
    c=0
    while elem in L:
      ind = L.index(elem)
      foo = L.pop(ind)
      c+=1
    if c>MaxCount:
      MaxCount=c
      ret = elem
  return ret



def numEle(lis, ele):
	count = 0
	for i in range(len(lis)):
		if(ele == lis[i]):
			count += 1
	return count

'''
N人、M種目
→１つ以上の種目を選ぶ

A(ij)：人 i が j 番目に好きなスポーツ
→みんな自分が最も好きなスポーツのみ参加

→「最も多くの人が参加しているスポーツの参加人数の最小値」
'''



input1s = input()
input1 = [int(i) for i in input1s.split(' ')]
N = input1[0] # 行
M = input1[1] # 列

# append を使って二次配列
arr = []
arrow = []
for i in range(N):
	input2s = input()
	input2 = [int(j) for j in input2s.split(' ')]
	arr.append(input2)
	arrow.append(0) # 後で使う矢印の配列

min_M = math.ceil(M/N)
count = 0
# 全ての配列に矢印を指して min_M と比べて、矢印をスライド

for i in range(M):
	arrow_point = []
	for j in range(N):
		arrow_point.append(arr[j][arrow[j]])
	max_ele = maxElem(arrow_point)
	
	for k in range(N):
		if(count != min_M):
			if(arr[k][arrow[k]] == max_ele):
				arrow[k] += 1
	
	#print (arrow_point)

	if(min_M == numEle(arrow_point, max_ele)):
		break

print (numEle(arrow_point, max_ele))

Submission status: Wrong Answer

Solution code:
def solve():
    n, m = list(map(int, input().strip().split()))
    
    pref = []
    pri = [[0]*m for x in range(m)]
    curr = [0]*m

    for i in range(n):
        pref.append(list(map(int, input().strip().split())))
        for j in range(m):
            pri[j][pref[i][j] - 1] += 1
            if pref[i][j] == m:
                curr[j] += 1
    
    currmax = max(curr)

    ignore = []

    for i in range(m):
        if curr[i] == currmax:
            ignore.append(i)

    currmin = currmax

    while len(ignore) < m:
        # print(curr, ignore)
        curr = [0]*m
        for i in range(n):
            currmax = 0
            index = 0
            for j in range(m):
                if j not in ignore and pref[i][j] > currmax:
                    currmax = pref[i][j]
                    index = j
            curr[index] += 1

        currmax = max(curr)
        if currmax < currmin:
            currmin = currmax

        for i in range(m):
            if curr[i] == currmax:
                ignore.append(i)

    return currmin

print(solve())

Submission status: Wrong Answer

Solution code:
import numpy as np

def sports_fes():
	import collections
	n, m = map(int, raw_input().split(" "))
	a = []
	for i in range(n):
		a.append(map(int, raw_input().split(" ")))
	a = np.array(a)

	def _find_minimun(mat, ans):
		if not mat.size:
			return ans
		else:
			max_values = np.argmax(mat, axis=1)
			c = collections.Counter(max_values)
			index_to_remove = max(c, key=c.get)
			ans = min(ans, c.get(index_to_remove))
			new_mat = np.delete(mat, [index_to_remove], axis=1)
			return _find_minimun(new_mat,ans)

	print _find_minimun(a, n)


if __name__ == "__main__":
	sports_fes()

Submission status: Wrong Answer

Solution code:
import numpy as np

def sports_fes():
	import collections
	n, m = map(int, raw_input().split(" "))
	a = []
	for i in range(n):
		a.append(map(int, raw_input().split(" ")))
	a = np.array(a)
	
	def _find_minimun(mat, ans):
		if not mat.any():
			return ans
		else:
			max_values = np.argmax(mat, axis=1)
			c = collections.Counter(max_values)
			index_to_remove = max(c, key=c.get)
			ans = min(ans, c.get(index_to_remove))
			new_mat = np.delete(mat, [index_to_remove], axis=1)
			return _find_minimun(new_mat,ans)

	print _find_minimun(a, n)


if __name__ == "__main__":
	sports_fes()

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split(' '))
A = [map(int, raw_input().split(' ')) for _ in xrange(N)]

answer = N
for _ in xrange(M):
    participants = [0] * M
    for n in xrange(N):
        participants[A[n][0]-1] += 1
    sport_with_max_participants = max(xrange(M), key=lambda x: participants[x])
    answer = min(answer, participants[sport_with_max_participants])
    print sport_with_max_participants, A
    for n in xrange(N):
        A[n].remove(sport_with_max_participants+1)

print answer

Submission status: Wrong Answer

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

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

ans = float('inf')
for i in range(M - 1):
    rank = [0] * M
    for j in range(N):
        rank[A[j][0] - 1] += 1
    ans = min(ans, max(rank))
    remove_idx = rank.index(max(rank)) + 1
    for j in range(N):
        A[j].remove(remove_idx)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
import random

random.seed(1)

# sys.stdin = open('b1.in')


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


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def gen():
    n = 301
    m = 300
    a = [[-1] * m for i in range(n)]
    for i in range(n):
        for j in range(m):
            a[i][j] = 1 + (i + j) % m
    return n, m, a


def gen2(n, m):
    a = [list(range(1, m + 1)) for i in range(n)]
    for i in range(n):
        random.shuffle(a[i])
    return a


def is_possible(l, n, m, a):
    s = set(range(1, m + 1))
    choice = [0] * n
    c = Counter()
    for i in range(n):
        c[a[i][0]] += 1
    while len(s) > 0:
        update = False
        for i in range(n):
            j = choice[i]
            x = a[i][j]
            if x not in s:
                c[x] -= 1
                while a[i][j] not in s:
                    j += 1
                choice[i] = j
                x = a[i][j]
                c[x] += 1
            if c[x] > l:
                s.remove(x)
                update = True
                break
        if not update:
            return True
    return False


def solve(n, m, a):
    left, right = 1, m
    if is_possible(left, n, m, a):
        return n
    while right - left > 1:
        mid = (left + right) // 2
        if is_possible(mid, n, m, a):
            right = mid
        else:
            left = mid
    return right


def solve2(n, m, a):
    pass

    pinf = float("inf")
    best = pinf
    roop = 0
    while True:
        if roop == m:
            break

        firstlist = []
        for r in a:
            tmp = r[0]
            firstlist.append(tmp)
        mode = -1
        count = -1
        for i in range(1, m + 1):
            tmp_c = firstlist.count(i)
            if tmp_c > count:
                mode = i
                count = tmp_c
        if mode == -1:
            break
        elif count <= best:
            best = count
        for r in a:
            r.remove(mode)
        roop += 1
    return best


def check():

    n_steps = 10000
    M = 10
    for i in range(n_steps):
        n = random.randint(1, 10)
        m = random.randint(1, 10)

        a = gen2(n, m)
        aa = [row[:] for row in a]
        r = solve(n, m, a)
        r2 = solve2(n, m, aa)
        if r != r2:
            print('found', r, r2)
            print(n,m)
            for row in a:
                print(row)
            return n, m, a
        if i % (n_steps // M) == 0:
            print(i)


def main():
    solve_it()
    # check()


def solve_it():
    debug = False
    if debug:
        n, m, a = gen()
    else:
        n, m = read_int_list()
        a = [read_int_list() for _ in range(n)]
    res = solve(n, m, a)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

N, M = input().split()
N = int(N)
M = int(M)
A = []

for j in range(N):
    A.append([int(i) for i in input().split()])

A1 = np.array(A)
itermax = 2**M
C = np.array([[0 for i in range(N)]])

for k in range(1,itermax):
    fmt = '{0:0' + str(M) + 'b}'
    s1 = fmt.format(k)
    B = np.array([int(s1[i]) for i in range(len(s1))]) * 9
    C = np.append(C, np.array([np.argmin(A1 + B, axis = 1)]), axis = 0)

C = C[1:]

tmp1 = []
for l in range(M):
    tmp1.append(list(np.sum(C == l, axis = 1)))

tmp2 = np.array(tmp1)
ans = np.min(np.max(tmp2, axis = 0))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

# sys.stdin = open('b1.in')


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


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def gen():
    n = 301
    m = 300
    a = [[-1] * m for i in range(n)]
    for i in range(n):
        for j in range(m):
            a[i][j] = 1 + (i + j) % m
    return n, m, a


def is_possible(l, n, m, a):
    s = set(range(1, m + 1))
    choice = [0] * n
    c = Counter()
    for i in range(n):
        c[a[i][0]] += 1
    while len(s) > 0:
        update = False
        for i in range(n):
            j = choice[i]
            x = a[i][j]
            if x not in s:
                c[x] -= 1
                while a[i][j] not in s:
                    j += 1
                choice[i] = j
                x = a[i][j]
                c[x] += 1
            if c[x] > l:
                s.remove(x)
                update = True
                break
        if not update:
            return True
    return False


def solve():
    debug = False
    if debug:
        n, m, a = gen()
    else:
        n, m = read_int_list()
        a = [read_int_list() for _ in range(n)]
    left, right = 1, m
    if is_possible(left, n, m, a):
        return left
    while right - left > 1:
        mid = (left + right) // 2
        if is_possible(mid, n, m, a):
            right = mid
        else:
            left = mid
    return right


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cnt = [[0 for _ in range(m + 1)] for _ in range(m)]
for i in range(m):
    for j in range(n):
        cnt[i][a[j][i]] += 1
l = 0
r = m + 1
while 1 < r - l:
    c = (l + r) // 2
    used = [True for _ in range(m)]
    for i in range(m):
        for j in range(m + 1):
            if c < cnt[i][j]:
                used[i] = False
                break
    smax = 0
    sport = [0 for _ in range(m)]
    for i in range(n):
        tmp = 0
        si = 0
        for j in range(m):
            if used[j] and tmp < a[i][j]:
                si = j
                tmp = a[i][j]
        sport[si] += 1
        smax = max(smax, sport[si])
    if smax <= c:
        r = c
    else:
        l = c
print(r)

Submission status: Wrong Answer

Solution code:
def solve(m, prefers):
    if m == 1:
        return len(prefers)

    items = [True] * (m + 1)
    competes = {i: 0 for i in range(1, m + 1)}
    persons = [set() for _ in range(m + 1)]

    for i, prefer in enumerate(prefers):
        item = prefer[-1]
        competes[item] += 1
        persons[item].add(i)

    ans = m
    remain = m
    while ans > m // remain:
        mx = max(competes.values())
        ans = min(ans, mx)
        del_items = [a for a, k in competes.items() if k == mx]
        for a in del_items:
            items[a] = False
            del competes[a]
        if not competes:
            return ans
        for a in del_items:
            ps_a = persons[a]
            for p in ps_a:
                prefer = prefers[p]
                while not items[prefer[-1]]:
                    prefer.pop()
                item = prefer[-1]
                persons[item].add(p)
                competes[item] += 1
        remain -= len(del_items)

    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: Wrong Answer

Solution code:
def solve(m, prefers):
    if m == 1:
        return len(prefers)

    items = [True] * (m + 1)
    competes = {i: 0 for i in range(1, m + 1)}
    persons = [set() for _ in range(m + 1)]

    for i, prefer in enumerate(prefers):
        item = prefer[-1]
        competes[item] += 1
        persons[item].add(i)

    ans = m
    remain = m
    while ans > m // remain:
        mx = max(competes.values())
        ans = min(ans, mx)
        del_items = [a for a, k in competes.items() if k == mx]
        for a in del_items:
            items[a] = False
            del competes[a]
        if not competes:
            return ans
        for a in del_items:
            ps_a = persons[a]
            for p in ps_a:
                prefer = prefers[p]
                while not items[prefer[-1]]:
                    prefer.pop()
                item = prefer[-1]
                persons[item].add(p)
                competes[item] += 1
        remain -= 1

    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: Wrong Answer

Solution code:
def main():
    n, m = [int(x) for x in raw_input().split()]
    a = [[int(x) - 1 for x in raw_input().split()] for i in xrange(n)]
    ans = n
    j = 0
    while j < m:
        cnt = [0] * m
        for ai in a:
            cnt[ai[0]] += 1
        mc = max(cnt)
        ans = min(ans, mc)
        for k in xrange(m):
            if cnt[k] == mc:
                j += 1
        for ai in a:
            if cnt[ai[0]] == mc:
                ai.pop(0)
    return ans


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
def solve(m, prefers):
    if m == 1:
        return len(prefers)

    items = [True] * (m + 1)
    competes = {i: 0 for i in range(1, m + 1)}
    persons = [set() for _ in range(m + 1)]

    for i, prefer in enumerate(prefers):
        item = prefer[-1]
        competes[item] += 1
        persons[item].add(i)

    ans = m
    remain = m
    while ans > m // remain:
        mx = max(competes.values())
        ans = min(ans, mx)
        del_items = [a for a, k in competes.items() if k == mx]
        for a in del_items:
            items[a] = False
            del competes[a]
        for a in del_items:
            ps_a = persons[a]
            try:
                for p in ps_a:
                    prefer = prefers[p]
                    while not items[prefer[-1]]:
                        prefer.pop()
                    item = prefer[-1]
                    persons[item].add(p)
                    competes[item] += 1
            except:
                break
        else:
            remain -= 1
            continue
        break

    return ans


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

print(solve(m, prefers))