Monday, May 14, 2012

[DRAFT]Codejam 2012: Round 1B

This was the first year I participated in Google Codejam. I passed the Qualification round which was far more difficult than I expected. I didn't pass the first round.

However I did write some interesting code to solve Problem C. It's in Python.
'''Output: Second line is one subset. Third line is another subset with the same sum.
####
For the big input, you cannot find the solution in time. 
However, you can make the script read a part of the input set and hope you will still ger a solution. 
Set slice_index to a value between 100 and 500 to do it.  Use None to read the whole input.
'''
slice_index = 50

import itertools

def solve(S):
    #S: the set of numbers 
    
    subsets = (c
               for length in range(1,len(S)+1)
               for c in itertools.combinations(S,length)
               )

    #finds a good subset pair
    result = ((i,j)
              for i,j in itertools.combinations(subsets,2)
              if sum(i)==sum(j)
              )
    

    print "Case #%d:" % (test_case) #, no_new_line
    try:
        a, b = next(result)
        print ' '.join(map(str, a))
        print ' '.join(map(str, b))
    except StopIteration:
        print 'Impossible'

#Input
fin = open('big.in', 'r')
fin.readline()
for test_case, line in enumerate(fin, 1):
    line = map(int, line.split())[1:slice_index]
    solve(line)
Here is the Contest Analysis.

No comments:

Post a Comment