Problem:


Score : 700 points

Problem Statement

Takahashi is hosting an sports meet.
There are N people who will participate. These people are conveniently numbered 1 through N.
Also, there are M options of sports for this event. These sports are numbered 1 through M.
Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.

Takahashi knows that Person i's j-th favorite sport is Sport A_{ij}.
Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.

Takahashi is worried that one of the sports will attract too many people.
Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized.
Find the minimum possible number of the participants in the sport with the largest number of participants.

Constraints

1 \leq N \leq 300

1 \leq M \leq 300

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

2

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

Sample Input 2

3 3
2 1 3
2 1 3
2 1 3

Sample Output 2

3

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



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


Sample output:
2

Submission status: Wrong Answer

Solution code:
def 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]
        try:
            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
        except:
            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))

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:
        try:
            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]
                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
        except:
            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))

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
    try:
        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]
                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
    except:
        pass

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

try:
    print(solve(m, prefers))
except:
    print()

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


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 solve():
    debug = False
    if debug:
        n, m, a = gen()
    else:
        n, m = read_int_list()
        a = [read_int_list() for _ in range(n)]
    first_choice = [a[i][0] for i in range(n)]
    if len(set(first_choice)) == n:
        return 1
    left, right = 1, m
    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:
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 is_possible(l, n, m, a):
    s = set(range(1, m + 1))
    while len(s) > 0:
        c = Counter()
        for i in range(n):
            for j in range(m):
                if a[i][j] in s:
                    c[a[i][j]] += 1
                    break
        ns = len(s)
        s = set(filter(lambda x: c[x] <= l, s))
        if len(s) == ns:
            return True
    return False


def solve():
    n, m = read_int_list()
    a = [read_int_list() for _ in range(n)]
    left, right = 1, m
    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:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def gcd(a,b):
    while b>0:
        a,b = b,a
        b %= a
    return a

def main():
    n,m = LI()
    a = [sorted(list(zip(LI(),range(m))),reverse=True) for _ in range(n)]
    ai = [0] * n
    d = collections.defaultdict(int)
    for i in range(n):
        d[a[i][0][1]] += 1
    dr = r = max(d.values())
    s = set(list(range(m)))
    while s:
        for k,v in d.items():
            if v == dr:
                s.remove(k)
        if not s:
            break
        d = collections.defaultdict(int)
        for i in range(n):
            while a[i][ai[i]][1] not in s:
                ai[i] += 1
        for i in range(n):
            d[a[i][ai[i]][1]] += 1
        dr = max(d.values())
        if r > dr:
            r = dr
    return r


print(main())

Submission status: Wrong Answer

Solution code:
def f(t, n, m, a, js, ans):
    if t == 0:
        return ans
    cnt = [0] * m
    for i in xrange(n):
        cnt[a[i][js[i]]] += 1
    mc = max(cnt)
    ans = min(ans, mc)
    return min(
        f(t - 1, n, m, a, [js[i] + (1 if a[i][js[i]] == k else 0) for i in xrange(n)], ans)
        for k in xrange(m) if cnt[k] == mc
    )


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


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

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
    for _ in xrange(m):
        cnt = [0] * m
        for ai in a:
            cnt[ai[0]] += 1
        c = max(xrange(m), key=lambda k: cnt[k])
        ans = min(ans, cnt[c])
        for ai in a:
            if ai[0] == c:
                ai.pop(0)
    return ans


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

Submission status: Time Limit Exceeded

Solution code:
N, M, *X = map(int, open(0).read().split())
A = []
for a in zip(*[iter(X)]*M):
  A.append(a)
log = set(range(1,M+1))
ans = []
while log:
  dic = {}
  for i in range(N):
    for j in range(M):
      if A[i][j] in log:
        dic[A[i][j]] = dic.get(A[i][j],0)+1
        break
  ans.append(max(dic.values()))
  c = max(dic.keys(),key=lambda x:dic[x])
  log.remove(c)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M, *X = map(int, open(0).read().split())
A = []
for a in zip(*[iter(X)]*M):
  A.append(a)
log = set(range(1,M+1))
ans = N
while log:
  dic = {}
  for i in range(N):
    for j in range(M):
      if A[i][j] in log:
        dic[A[i][j]] = dic.get(A[i][j],0)+1
        break
  ans = min(ans,max(dic.values()))
  c = max(dic.keys(),key=lambda x:dic[x])
  log.remove(c)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
A=[[0]*m for i in range(n)]
for i in range(n):
  A[i]=list(map(int,input().split()))
B=[0]*m
for i in range(n):
  B[A[i][0]-1]+=1
a=0
b=0
for i in range(m):
  if B[i]>a:
    a=B[i]
    b=i
ans=a
C=[b+1]
i=0
while i<=m-2:
  B=[0]*m
  for j in range(n):
    for k in range(m):
      if A[j][k] not in C:
        B[A[j][k]-1]+=1
        break
  a=0
  b=0
  for l in range(m):
    if B[l]>a:
      a=B[l]
      b=l
  if ans>a:
    ans=a
  C.append(b+1)
  i=i+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = [None] * N
for i in range(N):
    A[i] = [a-1 for a in LIST()]

k = 0
valid = [1] * M
ans = INF
while k < M:
    cnt = [0] * M
    for i in range(N):
        mn = INF
        idx = -1
        for j in range(M):
            a = A[i][j]
            if valid[a]:
                # まだ有効なもののうち、一番順位が高いスポーツにカウント追加
                cnt[a] += 1
                break
    mx = max(cnt)
    ans = min(ans, mx)
    for i in range(M):
        # 票が最も集まったものは次から選ばないようにする
        if cnt[i] == mx:
            valid[i] = 0
            k += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as C
N, M = map(int, input().split())
A = [[int(a)-1 for a in input().split()] for _ in range(N)]
X = [1] * M
mi = 1 << 100
for i in range(M):
    c = C([min([(j, A[i][j]) for j in range(M) if X[A[i][j]]])[1] for i in range(N)])
    a = max([(c[a], a) for a in c])[1]
    mi = min(mi, c[a])
    X[a] -= 1
print(mi)

Submission status: Time Limit Exceeded

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

n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
cnt = [0] * (m + 1)
cur = [0] * n
done = [0] * (m + 1)
for i in range(n):
	cnt[a[i][0]] += 1
	cur[i] = a[i][0]
ans = max(cnt)
for _ in range(m - 1):
	col = cnt.index(max(cnt))
	done[col] = 1
	for i in range(n):
		if cur[i] == col:
			j = 0
			while done[a[i][j]]:
				j += 1
			t = a[i][j]
			cur[i] = t
			cnt[t] += 1
	cnt[col] = 0
	ans = min(ans, max(cnt))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 解説
# https://img.atcoder.jp/agc018/editorial.pdf

def solve():
    import sys

    sys.setrecursionlimit(10 ** 7)

    def simulate(use: set):
        if not use:
            return n
        cnt = [0] * m
        for player in a:
            for sport in player:
                if sport in use:
                    cnt[sport] += 1
                    break
        max_ = max(cnt)
        ret = max_
        for j, c in enumerate(cnt):
            if c == max_:
                ret = min(ret, simulate(use - {j}))
        return ret

    n, m = map(int, input().split())
    a = [tuple(int(x) - 1 for x in input().split()) for _ in range(n)]
    use = set(range(m))
    return simulate(use)


print(solve())

# まず、すべてのスポーツを実施すると仮定して、どのスポーツに何人の人が参加するか調べます。
# ここで、最も参加人数の多いスポーツがスポーツ P で、その参加人数が Q 人だとします。
# 当り前ですが、この段階で、参加人数の最大値が Q の解が得られています。
# よって、あと考えるべきは、参加人数の最大値が Q 未満の解です。
# そして、そのような解において、スポーツ P は必ず実施されません。
# なので、Q 未満の解を求めるには、スポーツ P を候補から外した上で、
# もう一度同じ問題を解きなおせばよいです。
# これを再帰的に行っていけば、最終的な解を求めることができます。
# 再帰するごとに候補の数が減るので、再帰は合計 M 回呼ばれます。
# 誰がどのスポーツを選ぶのか愚直にシミュレーションすると O(NM) かかるので、
# 全体で O(NM2) でこの問題は解けました。
# なお、シミュレーションを工夫すると、全体で O(NM) でこの問題を解くこともできます。

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
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:
    sports_cand = Q.pop(0)
    n_sports = [0]*M
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                n_sports[mat[n][m]] += 1
                break
    max_val = max(n_sports)
    for i, v in enumerate(n_sports):
        if v == max_val:
            ans = min(ans, v)
            if len(sports_cand) > 2:
                Q.append(sports_cand-{i})
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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)])]
checked = {"".join([str(v) for v in range(M)])}
while Q:
    sports_cand = Q.pop(0)
    n_sports = [0]*M
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                n_sports[mat[n][m]] += 1
                break
    max_val = max(n_sports)
    for i, v in enumerate(n_sports):
        if v == max_val:
            ans = min(ans, v)
            next_state = "".join(map(str, list(sorted(list(sports_cand-{i})))))
            if len(sports_cand) > 2 and not next_state in checked:
                Q.append(sports_cand-{i})
                checked.add(next_state)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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)])]
checked = {"".join([str(v) for v in range(M)])}
while Q:
    sports_cand = Q.pop(0)
    n_sports = [0]*M
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                n_sports[mat[n][m]] += 1
                break
    max_val = max(n_sports)
    for i, v in enumerate(n_sports):
        if v == max_val:
            ans = min(ans, v)
            next_state = "".join(map(str, sports_cand-{i}))
            if len(sports_cand) > 2 and not next_state in checked:
                Q.append(sports_cand-{i})
                checked.add(next_state)
print(ans)

Submission status: Time Limit Exceeded

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:
    sports_cand = Q.pop(0)
    n_sports = [0]*M
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                n_sports[mat[n][m]] += 1
                break
    max_val = max(n_sports)
    for i, v in enumerate(n_sports):
        if v == max_val:
            ans = min(ans, v)
            if len(sports_cand) > 2:
                Q.append(sports_cand-{i})
print(ans)

Submission status: Time Limit Exceeded

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:
                num = tmp_val
            elif tmp_val >= num:
                num = tmp_val
                ans = min(ans, tmp_val)
                ans = min(ans, rec(sports_cand, ans))
                sports_cand.remove(sports_cand - {id})
            else:
                break
    return ans


print(rec(sports_cand, ans))

Submission status: Time Limit Exceeded

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 id == -1:
                continue
            if tmp_val >= num:
                num = tmp_val
                ans = min(ans, tmp_val)
                ans = min(ans, rec(sports_cand, ans))
                sports_cand.remove(sports_cand - {id})
            else:
                break
    return ans


print(rec(sports_cand, ans))

Submission status: Time Limit Exceeded

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=[]
  append=p.append
  for i in range(n):
    for j in range(m):
      aij=A[i][j]
      if D[aij]==1:
        append(aij)
        break
  a,b=collections.Counter(p).most_common()[0]
  if b<ans:
    ans=b
  D[a]=0
print(ans)

Submission status: Time Limit Exceeded

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
  a,b=collections.Counter(p).most_common()[0]
  if b<ans:
    ans=b
  D[a]=0
print(ans)

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

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
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):
        idx=0
        while delete[A[i][idx]]:
            idx+=1
        B[i]=A[i][idx]
print(ans)

Submission status: Time Limit Exceeded

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

s = set([])
ans = n
for _ in range(m):
    memo = [0] * (m + 1)
    flag = [0] * (n + 1)
    for j in range(m):
        for i in range(n):
            if flag[i] == 0 and a[i][j] not in s:
                memo[a[i][j]] += 1
                flag[i] = 1
    idx = 0
    mmax = 0
    for i, me in enumerate(memo):
        if me > mmax:
            idx = i
            mmax = me

    ans = min(ans, mmax)
    s.add(idx)

print(ans)

Submission status: Time Limit Exceeded

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

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

left = 0
right = n
while right - left > 1:
    mid = (right + left) // 2
    ans = False
    s = set([i+1 for i in range(m)])
    while s:
        memo = [0] * (m + 1)
        flag = [0] * (n + 1)
        for j in range(m):
            for i in range(n):
                if flag[i] == 0 and a[i][j] in s:
                    memo[a[i][j]] += 1
                    flag[i] = 1
                    if memo[a[i][j]] > mid:
                        s.remove(a[i][j])
                        break
            else:
                continue
            break

        if max(memo) <= mid:
            ans = True
            break
    if ans:
        right = mid
    else:
        left = mid
print(right)

Submission status: Time Limit Exceeded

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

left = 0
right = n
while right - left > 1:
    mid = (right + left) // 2
    ans = False
    s = set([i+1 for i in range(m)])
    while s:
        memo = [0] * (m + 1)
        flag = [0] * (n + 1)
        for j in range(m):
            for i in range(n):
                if flag[i] == 0 and a[i][j] in s:
                    memo[a[i][j]] += 1
                    flag[i] = 1
                    if memo[a[i][j]] > mid:
                        s.remove(a[i][j])
                        break
            else:
                continue
            break

        if max(memo) <= mid:
            ans = True
            break
    if ans:
        right = mid
    else:
        left = mid
print(right)

Submission status: Time Limit Exceeded

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

for k in range(1, n+1):
    s = set([i+1 for i in range(m)])
    while s:
        memo = [0] * (m + 1)
        flag = [0] * (n + 1)
        for j in range(m):
            for i in range(n):
                if flag[i] == 0 and a[i][j] in s:
                    memo[a[i][j]] += 1
                    flag[i] = 1
                    if memo[a[i][j]] > k:
                        s.remove(a[i][j])
                        break
            else:
                continue
            break
        if max(memo) <= k:
            print(k)
            exit()

Submission status: Time Limit Exceeded

Solution code:
a,b = map(int,input().split(" "))
ar = []
br = [i+1 for i in range(b)]
cr = [0 for i in range(b)]
for i in range(a):
    l = list(map(int,input().split(" ")))
    ar.append(l)
mi = 300
while len(br) != 0:
    for r in ar:
        for l in r:
            if l in br:
                cr[l-1] += 1
                break
    mi = min(max(cr),mi)
    br.remove(cr.index(max(cr))+1)
    cr = [0 for i in range(b)]
print(mi)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,m = map(int, input().split())
l = []
for i in range(n):
    l.append(list(map(int, input().split())))

ans = n
join_num = np.zeros(m).astype(np.int32)
for i in range(m):
    join_num = np.zeros(m).astype(np.int32)
    for j in range(n):
        join_num[l[j][0]-1]+=1
    tmp = join_num.argmax()
    ans = min(ans,join_num[tmp])
    for j in range(n):
        l[j].remove(tmp+1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
A=[[int(i) for i in input().split()] for _ in range(N)]
R=[0 for i in range(M)]
for i in range(N):
    R[A[i][0]-1]+=1
P=0
Q=0
S=set()
for i in range(N):
    if R[A[i][0]-1]>=P:
        P=R[A[i][0]-1]
        Q=A[i][0]
S.add(Q)
while(len(S)<M):
    Pd=0
    Qd=0
    R=[0 for i in range(M)]
    for i in range(N):
        for j in range(M):
            if not(set([A[i][j]]) <= S):
                R[A[i][j]-1]+=1
                break
    for j in range(M):
        if R[j]>=Pd:
            Pd=R[j]
            Qd=j+1
    if Pd<P:
        P=Pd
    S.add(Qd)
print(P)

Submission status: Time Limit Exceeded

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()))
    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: Time Limit Exceeded

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

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

exl = []
ex = tuple(exl)
# 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))
    exl.append(p.index(max(p)))
    ex = tuple(exl)
    # print(p)
    # print(ex)

print(ans)

Submission status: Time Limit Exceeded

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)))
    # print(p)
    # print(ex)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter

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

A = np.array(A)
result = N

for j in range(M):
	top = A[:,0]
	c = Counter(list(top))
	most = c.most_common(1)
	mode = most[0][0]
	count = most[0][1]

	if result > count:
		result = count
	if count == 1:
		break

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

print(result)