-
Notifications
You must be signed in to change notification settings - Fork 14
/
lollipop-shop.py
43 lines (39 loc) · 1.19 KB
/
lollipop-shop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright (c) 2018 kamyu. All rights reserved.
#
# Google Code Jam 2018 Round 1C - Problem B. Lollipop Shop
# https://codingcompetitions.withgoogle.com/codejam/round/0000000000007765/000000000003e068
#
# Time: O(N^2)
# Space: O(N)
#
import sys
import random
def sell(statistics, stock, prefs):
if prefs:
for i in prefs:
statistics[i] += 1
least_flavors = []
for i in prefs:
if i in stock:
if not least_flavors or \
statistics[least_flavors[0]] > statistics[i]:
least_flavors = [i]
elif statistics[least_flavors[0]] == statistics[i]:
least_flavors.append(i)
if least_flavors:
i = random.randint(0, len(least_flavors)-1)
stock.discard(least_flavors[i])
print least_flavors[i]
sys.stdout.flush()
return
print -1
sys.stdout.flush()
def lollipop_shop():
N = input()
stock = set(range(N))
statistics = [0]*N
for _ in xrange(1, N+1):
prefs = map(int, raw_input().strip().split())
sell(statistics, stock, prefs[1:])
for case in xrange(input()):
lollipop_shop()