In [1]:
# Version 1
# Only works for test cases 1-3
# FAILS on time for test cases 4+
In [2]:
# read in N,M
fin = open ('cereal.in', 'r')
fout = open ('cereal.out', 'w')
# N = Cows
# M = Cereals
N,M = map(int, fin.readline().split())
print(N,M)
In [3]:
# read in first choice, second choice
choice1 = []
choice2 = []
# subtract 1 so cereal index is 0 to M-1
for i in range(N):
a,b = map(int,fin.readline().split())
choice1.append(a-1)
choice2.append(b-1)
# print out choice lists
for a,b in zip(choice1,choice2):
print(a,b)
In [4]:
ans = [0]*N
# simulation of cereal assignment with a nested loop
# outer loop chooses cow i to be first cow in the line
# inner loop steps from cow i to the end of the line
for i in range(N):
cereal = [0]*M # check if cereal has been consumed
for j in range(i,N):
c1 = choice1[j]
c2 = choice2[j]
# check if first choice is available
if cereal[c1] == 0:
cereal[c1] = 1
ans[i] += 1
# check if second choice is available
elif cereal[c2]==0:
cereal[c2] = 1
ans[i] += 1
print(ans)
for i in range(N):
fout.write(str(ans[i])+"\n")
fout.close()