import codejam as gcj import codejam.graphs as gph T = gcj.read_input('i') for t in range(T): W, H, B, buildings = gcj.read_input('i i i->', 'i[]') G = gph.grid_2d_graph(W, H) for x in range(W): G.add_edge('S', (x, 0)) G.add_edge('T', (x, H - 1)) for a, b in G.edges(): G[a][b]['capacity'] = 1 for l, b, r, top in buildings: for i in range(l, r + 1): for j in range(b, top + 1): G.remove_node((i, j)) print 'Case #%i:' % (t + 1), gph.node_connectivity(G, 'S', 'T')
import codejam as gcj import codejam.maths as mth T = gcj.read_input('i') for t in range(T): N, X, Y = gcj.read_input('i i i') X = abs(X) l = (X + Y) / 2 if X == 0: Z = (l + 1) * (2 * l + 1) if N >= Z: answer = 1.0 else: answer = 0.0 else: N -= l * (2 * l - 1) if N < Y + 1: answer = 0.0 elif N >= l * 2 + Y + 1: answer = 1.0 else: # probability at least Y heads from N answer = 0.0 for i in range(Y + 1, N + 1): answer += mth.choose(N, i) answer *= 0.5**N print "Case #%d:" % (t + 1), answer
import codejam as gcj T = gcj.read_input('i') for t in range(T): a, A, b, B = gcj.read_input('i', 4, 'i[]', 'i', 4, 'i[]') possible = [x for x in A[a - 1] if x in B[b - 1]] if len(possible) == 0: answer = 'Volunteer cheated!' elif len(possible) > 1: answer = 'Bad magician!' else: answer = possible[0] print 'Case #%i:' % (t + 1), answer
import codejam as gcj T = gcj.read_input('i') for t in range(T): N, X, S = gcj.read_input('i i', 'i[<]') discs, i, j = 0, 0, N - 1 while i <= j: if S[i] + S[j] <= X: i += 1 j -= 1 discs += 1 print 'Case #%i:' % (t + 1), discs
import codejam as gcj gcj.load_input('B-large.in') T = gcj.read_input('i') for t in range(T): S = gcj.read_input('s') c, parts = '', [] for s in S: if c != s: parts += [s] c = s if parts[-1] == '+': parts.pop() print 'Case #%i:' % (t + 1), len(parts)
import codejam as gcj gcj.load_input('A-large.in') T = gcj.read_input('i') for t in range(T): N = gcj.read_input('i') if N == 0: answer = 'INSOMNIA' else: digits = set() M = N while len(digits) < 10: answer = M digits.update(list(str(M))) M += N print 'Case #%i:' % (t + 1), answer
c += b return c def convert(s, pattern): counts = [1] * len(pattern) i = 0 for a, b in itr.window(s, 2): if a == b: counts[i] += 1 else: i += 1 return counts T = gcj.read_input('i') for t in range(T): N, words = gcj.read_input('i->', 's') unique_compressed_words = set(map(compressed, words)) if len(unique_compressed_words) == 1: pattern = unique_compressed_words.pop() words = map(lambda s: convert(s, pattern), words) answer = 0 for i in range(len(pattern)): chunks = list(sorted(word[i] for word in words)) best_cost = 10**9 for target in range(chunks[0], chunks[-1] + 1): costs = map(lambda x: abs(x - target), chunks) best_cost = min(best_cost, sum(costs)) answer += best_cost
# new_trips.append([old_start, old_end, old_people - swaps]) # new_trips.append([new_start, old_end, swaps]) # new_trips.append([old_start, new_end, swaps]) # else: # new_trips.append([old_start, old_end, old_people]) # if new_people > 0: # new_trips.append([new_start, new_end, new_people]) # trips_in_prog += new_trips # # print "Case #%d:" % (t + 1), saving import codejam as gcj MOD = 1000002013 T = gcj.read_input('i') for t in range(T): N, M = gcj.read_input('i i') trips_to_make = [] for ii in range(M): trips_to_make.append(gcj.read_input('i[]')) trips_to_make.sort() saving = 0 while trips_to_make != []: trips_to_make.sort(key = lambda tup: tup[0], reverse = True) new_start, new_end, new_people = trips_to_make.pop() if new_start == new_end: # print trips_to_make, new_start, new_end, new_people assert(False) continue poss_swap_people = filter(lambda tup: tup[0] > new_start and tup[0] <= new_end and tup[1] > new_end, trips_to_make) # print new_start, new_end, new_people
import codejam as gcj gcj.load_input('C-large.in') T = gcj.read_input('i') for t in range(T): N, J = gcj.read_input('i i') print 'Case #%i:' % (t + 1) for n in xrange(2**N): coin = '1%s1' % bin(n)[2:].zfill(N - 2) divisors = [] for base in range(9): value = int(coin, base + 2) for p in [2, 3, 5, 7, 11]: if value % p == 0: divisors += [p] break else: break else: print coin, ' '.join(map(str, divisors)) J -= 1 if J == 0: break
import codejam as gcj import codejam.maths as mth T = gcj.read_input('i') for t in range(T): P, Q = map(int, gcj.read_input('s').split('/')) d = mth.gcd(P, Q) P, Q = P / d, Q / d f = mth.factorisation(Q) if P == Q == 1: answer = 0 elif len(f) == 1 and 2 in f: answer = f[2] while P >= 2: P = P / float(2) answer -= 1 else: answer = 'impossible' print 'Case #%i:' % (t + 1), answer
import codejam as gcj T = gcj.read_input('i') for t in range(T): C, F, X = gcj.read_input('f f f') time, rate = 0.0, 2.0 while True: wait_time = X / rate buy_time = (C / rate) + (X / (rate + F)) if wait_time < buy_time: time += wait_time break else: time += (C / rate) rate += F print 'Case #%i:' % (t + 1), time
import codejam as gcj import codejam.graphs as gph gcj.load_input('C-large.in') T = gcj.read_input('i') for t in range(T): N, pairs = gcj.read_input('i->', 's s') G = gph.Graph() G.add_edges_from(((0, a), (1, b)) for a, b in pairs) matching = gph.bipartite.maximum_matching(G) print 'Case #%i:' % (t + 1), N - (len(G) - (len(matching) / 2))
import codejam as gcj T = gcj.read_input('i') for t in range(T): A, B, K = gcj.read_input('i i i') digits = bin(K)[2:] total = 0 for a in range(A): for b in range(B): if a & b < K: total += 1 print 'Case #%i:' % (t + 1), total
import codejam as gcj import codejam.datastructures as dst def cost(o, e): return sum(N - i for i in range(e - o)) T = gcj.read_input('i') for t in range(T): N, M, J = gcj.read_input('i i->', 'i i i') starts, ends, points = dst.defaultdict(int), dst.defaultdict(int), [] simple = [] full_cost, min_cost = 0, 0 for o, e, p in J: points += [o, e] starts[o] += p ends[e] += p full_cost += p * cost(o, e) points = sorted(set(points)) blocks = [] h = 0 for i, x in enumerate(points[:-1]): h += (starts[x] - ends[x]) blocks += [[points[i + 1] - x, h]] blocks += [[0, 0]] while blocks != []: dist, height = 0, 10**10
import codejam as gcj T = gcj.read_input('i') for t in range(T): D, N, horses = gcj.read_input('i i->', 'i i') time_to_D = [float(D - K) / S for K, S in horses] print 'Case #%i:' % (t + 1), D / max(time_to_D)
import codejam as gcj import codejam.iterators as itr T = gcj.read_input('i') for t in range(T): R, C, M = gcj.read_input('i i i') grid = [list('.' * C) for r in range(R)] grid[-1][-1] = 'c' if R == 1 or M == 0: for m in range(M): grid[0][m] = '*' elif C == 1: for m in range(M): grid[m][0] = '*' elif M == R * C - 1: grid = [list('*' * C) for r in range(R)] grid[-1][-1] = 'c' else: gap = (M - (R - 2) * (C - 2)) % 2 for n, m in itr.multirange(R - 2, C - 2): if M > 0: grid[n][m] = '*' M -= 1 if gap and M > 0 and R > 2 and C > 2: grid[R - 3][C - 3] = '.' M += 1 for c in range(C - 2 - gap): if M >= 2:
import codejam as gcj T = gcj.read_input('i') for t in range(T): S, K = gcj.read_input('s i') S = list(S) flip, flips = {'+': '-', '-': '+'}, 0 for i in range(len(S) - K + 1): if S[i] == '-': flips += 1 S[i:i + K] = [flip[s] for s in S[i:i + K]] if '-' in S: answer = 'IMPOSSIBLE' else: answer = flips print 'Case #%i:' % (t + 1), answer
import codejam as gcj import codejam.iterators as itr import codejam.graphs as gph def fact(n): f = 1 for i in range(1, n + 1): f *= i return f T = gcj.read_input('i') for t in range(T): N, cars = gcj.read_input('i', 's[<]') alphabet = sorted(set(''.join(cars))) counts = {l: [0, 0, 0] for l in alphabet} pure = [c for c in cars if len(set(c)) == 1] cars = [c for c in cars if c not in pure] for i, c in enumerate(cars): compressed = c[0] for x, y in itr.window(c, 2): if x != y: compressed += y cars[i] = compressed #print cars G = gph.DiGraph() G.add_nodes_from(alphabet) for c in cars: