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: Runtime Error

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

Submission status: Runtime Error

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
    rem_key = {}
    for key, val in C.most_common():
        if val >= tmp:
            tmp = val
            ans = min(ans, val)
            rem_key.add(key)
        else:
            break
    if len(sports_cand) > 2:
        Q.append(sports_cand-rem_key)
print(ans)

Submission status: Runtime Error

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(M)]

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'''
            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: Runtime Error

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(M)]

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: Runtime Error

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(N):
    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: Runtime Error

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
    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: Runtime Error

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

Submission status: Runtime Error

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: Runtime Error

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

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

ex = []
# print(a)
ans = 999
while len(ex) < m:
    # print('aaa')
    p = [0]*(m+1)
    for i in range(n):
        for j in range(m):
            if a[i][j] in ex:
                continue
            else:
                p[a[i][j]] +=1
                break
    ans = min(ans,max(p))
    ex.append(p.index(max(p)))
    ex = tuple(ex)
    # print(p)
    # print(ex)

print(ans)

Submission status: Runtime Error

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)
    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: Runtime Error

Solution code:
import numpy as np
from scipy import stats

input1 = input("").split(" ")
N = int(input1[0])
M = int(input1[1])
temp = [ input() for _ in range(N) ]
A = []
for i in range(N):
	A.append(list(map(int,temp[i].split(" "))))

A = np.array(A)
result = N

for j in range(M):
	top = A[:,0]
	mode = stats.mode(top)
	count = len(np.where(top == mode)[0])
	if result > count:
		result = count

	for i in range(N):
		temp = np.delete(A[i], np.where(A[i] == mode)[0])
		A[i] = np.append(temp,0)

print(result)

Submission status: Runtime Error

Solution code:
import pandas as pd


def most_popular(df):
    maxs = df.idxmax(axis=1)
    counts = maxs.value_counts(sort=True, ascending=False)
    return counts.index[0], counts.values[0]


line = raw_input()
man_num = int(line.split(" ")[0])
sports_num = int(line.split(" ")[1])
table = pd.DataFrame()
for i in xrange(0, man_num):
    prefs = [int(s) for s in raw_input().split(" ")]
    table = table.append(pd.Series(prefs, name=i))
min_population = man_num
while table.columns.size > 0:
    mp = most_popular(table)
    print mp
    min_population = min(min_population, mp[1])
    table = table.drop(mp[0], axis=1)
print min_population

Submission status: Runtime Error

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

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

print answer

Submission status: Runtime Error

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))
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: Runtime Error

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

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

    for i, ps in enumerate(prefers):
        item = ps[-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:
            pa = persons[a]
            for p in pa:
                pf = prefers[p]
                while not items[pf[-1]]:
                    pf.pop()
                item = pf[-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: Runtime Error

Solution code:
n, m = map(int, input().split())
prefers = [list(reversed(list(map(int, input().split())))) for _ in range(n)]
items = [True] * (m + 1)
competes = {i: 0 for i in range(1, m + 1)}
persons = [set() for _ in range(m + 1)]

for i, ps in enumerate(prefers):
    item = ps[-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:
        pa = persons[a]
        try:
            for p in pa:
                pf = prefers[p]
                while not items[pf[-1]]:
                    pf.pop()
                item = pf[-1]
                persons[item].add(p)
                competes[item] += 1
        except:
            break
    remain -= 1

print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
prefers = [list(reversed(list(map(int, input().split())))) for _ in range(n)]
items = [True] * (m + 1)
competes = {i: 0 for i in range(1, m + 1)}
persons = [set() for _ in range(m + 1)]

for i, ps in enumerate(prefers):
    item = ps[-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:
        pa = persons[a]
        for p in pa:
            pf = prefers[p]
            try:
                while not items[pf[-1]]:
                    pf.pop()
            except:
                break
            item = pf[-1]
            persons[item].add(p)
            competes[item] += 1
    remain -= 1

print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
prefers = [list(reversed(list(map(int, input().split())))) for _ in range(n)]
items = [True] * (m + 1)
competes = {i: 0 for i in range(1, m + 1)}
persons = [set() for _ in range(m + 1)]

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

ans = m
remain = m
while 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:
        pa = persons[a]
        for p in pa:
            pf = prefers[p]
            while not items[pf[-1]]:
                pf.pop()
            item = pf[-1]
            persons[item].add(p)
            competes[item] += 1
    remain -= 1

print(ans)

Submission status: Runtime Error

Solution code:
d = {}


def f(s, n, m, a, js):
    if s in d:
        return d[s]
    cnt = [0] * m
    for i in xrange(n):
        cnt[a[i][js[i]]] += 1
    mc = max(cnt)
    d[s] = min(mc, min(
        f(s | {k}, n, m, a, [js[i] + (1 if a[i][js[i]] == k else 0) for i in xrange(n)])
        for k in xrange(m) if cnt[k] == mc
    ))
    return d[s]


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)]
    d[frozenset(xrange(m))] = n
    return f(frozenset(), n, m, a, [0] * n)


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

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
prefers = [list(reversed(list(map(int, input().split())))) for _ in range(n)]
items = [True] * (m + 1)
competes = {i: 0 for i in range(1, m + 1)}
persons = [set() for _ in range(m + 1)]

for i, ps in enumerate(prefers):
    item = ps[-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
    for a in del_items:
        pa = persons[a]
        for p in pa:
            pf = prefers[p]
            while not items[pf[-1]]:
                pf.pop()
            item = pf[-1]
            persons[item].add(p)
            competes[item] += 1
        persons[a] = None
        del competes[a]
    remain -= 1

print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
prefers = [list(reversed(list(map(int, input().split())))) for _ in range(n)]
items = [True] * (m + 1)
competes = {i: 0 for i in range(1, m + 1)}
persons = [set() for _ in range(m + 1)]

for i, ps in enumerate(prefers):
    item = ps[-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
        pa = persons[a]
        for p in pa:
            pf = prefers[p]
            while not items[pf[-1]]:
                pf.pop()
            item = pf[-1]
            persons[item].add(p)
            competes[item] += 1
        persons[a] = None
        del competes[a]
    remain -= 1

print(ans)