TCO14 R2A

So I barely managed 615th out of the 1439 people who registered out of the 2500 potential people.  My rating didn’t move down much as a result, but I was disappointed with my result all the same.  Getting the second problem out was good for a guaranteed t-shirt, and solving the 1000pt was a guaranteed advancement.  Advancement cut-off was 442 points.

Q1) Given 16 values arrange them into a 4×4 grid such that sum of all edge values + double the corners + the absolute difference between neighbour pairs is maximal.  Return the maximal sum.

A1) I am not a fan of this question.  I solved it quickly, but accidentally, with a poor solution which is not at all obviously correct.  I then spent far too long trying to convince myself as to whether I was missing something critical, getting a low score in the process.

So the trick to this question is to convince yourself that the values given do not matter, if you sort them and place them the same as you would place 1 through 16 optimally, you still get an optimal solution.  One such ordering is as follows:

16  3 15  8
 4 14  1 13
12  2 11  5
 7 10  6  9

The idea is that if you choose to have your largest 8 numbers in a checker board pattern the sum becomes 4 * sum of largest 8 – 4 * sum of the middle 2 (because they are surrounded) – 2 * sum of those on the edges (because their being on the edge offsets one of their 3 neighbours) – 0 * sum of those in the corners (because corner offsets both of the 2 neighbours).  So it smallest 2 numbers go in the middle, next 4 smallest on the edges, and the next 2 in the corners, and the rest are checker boarded is optimal for the checker board scenario.  A small inspection shows that the formula still holds if some of the smallest 8 are equal to some of the largest 8.  All that remains is to prove that there is no scenario where 2 elements of the largest 8 are neighbours produces a better result.  But I have no idea how to do that and the checker board pattern is somewhat ‘obviously’ good.

As a demonstration of why this problem was ‘broken’ – placing the numbers into the 4×4 square in sorted order (but not for all random orders) and them performing a greedy pairwise swap until no pairwise swaps improve the sum, would pass system tests, despite having practically no relation to the actual greedy arrangement.

Q2) Given a set of positions in a long narrow hallway, determine the minimum they have to move in order to get to a different set of positions, if they can only pass each other at either end of the hallway.

A2) This is where I am really disappointed with my self.  Even with all of the time wasting I did on the first problem I still had > 25minutes left after my submission.  However because I had become convinced that I was missing something with that first question, and no one else in the room had submitted the 500pt, and one person had skipped it in favour of the 1000pt, I decided it was a better investment in my time to try and come up with a killer test case to break my (and hopefully others) 250pt question.  Which was both a) futile since everyone else’s code passed system tests and b) unsuccessful, since my solution passed system tests…

So this problem really isn’t that difficult.  Sort by starting locations to get an ordered set of indexes, sort by ending locations to get another ordered set of indexes.  Now there are 2 basic scenarios.

First up, some sub-range of the starting indexes which all have the same destination indexes directly shifts to their destinations while everything to the left shifts via the left end, and everything right shifts via the right end.  This is only valid if every item to the left has a destination on the left, and every item on the right has a destination on the right.  This has O(N^2) scenarios each with an evaluation time of O(N) to give O(N^3).

The second is a bit more complicated and I might have gotten it wrong (if there wasn’t a practice question which would have picked it up).  In this case no elements move directly to their destination, everything moves either via the left or the right ends.  So for every possible partition point you can consider everything going left and right, and getting to their destinations via the corresponding end points.  However this doesn’t work if the set of destination indexes of the left and right sections aren’t also on the left and right.  The trick is that these scenarios can all work, just that some items have to visit both ends.

So having partitioned the starting indexes into left and right groups, you also have to partition the destination indexes into left and right groups, and if they change groups, you have to add the extra distance of having visited both ends.  This gives O(N^2) scenarios each with an evaluation time of O(N) to add an addition O(N^3).  Input constraints were at most 50 items, so the result is easily managed.

Q3) Given a tree with black disks on some (non-root) nodes and a red disk on the root node, determine which nodes the red disk can visit by sliding along edges, if disks cannot pass through each other, but are otherwise all free to move along edges.

A3) Only 6 people solved this successfully, and none of the solutions are trivial to read and understand.  General idea seems to be that you cache the number of black disks and the number of nodes for each sub-tree and then use that to determine whether the black disks can be shuffled out of the way to let the red disk to visit.  If a node has multiple children and can be visited by the red disk, and not all of those children are filled, the red disk can visit some of the children, and potentially the black disks in the other children can be shifted up and into available spaces else where on the tree, allowing the red disk to reach even more locations.  I think this logic gets applied repeatedly until no more red disk reach locations can be found.  Without the cache the solution would be too slow.

GCJ14 R1C

Top 1000 advanced which looks to be solving question 1 completely and question 2 small in < 1:50.  For safety add in the question 3 small input.  In fact you could drop question 1 large.  All 3 small inputs was sufficient to advance.

Q1) Given a fraction, determine if it can be created by repeated pairwise averaging of 2^40 values which are either 0 or 1.  If so, determine the best case depth of a value of 1 in that averaging. (Best case being closest to the final value.)

A1) Odd little question, but actually quite simple.  Difference between small and large input was that the large could involve 64 bit numbers, and you needed to implement a GCD to reduce the fraction to minimal components, but otherwise still quite simple.

The solution comes that because repeated pairwise averaging is identical to just averaging all the values at once, the fraction must be of the form n/2^40.  So if the base of the fraction is a power of 2, the fraction can be created.  This is where the GCD comes in for the large input, without reducing to the minimal form, the base could be a multiple of a power of 2, and also of some other number, which can be factored out of the numerator.

For calculating the depth, it is obvious that if you consider the n/2^40 form you have n 1’s at the top level.  To keep a 1 as long as possible you shift them all to one side.  Thus the depth will be 40 – the floor of log 2 of n.  Another way of looking at it is that if the fraction >= 1/2 a parent can be a 1, >= 1/4 a grand parent, and so on.  So the depth is equal to the number of times you can double the fraction before it is greater than or equal to 1.

Q2) Given sequences of characters, determine how many ways the sequences can be ordered such that the concatenation has every case of any given letter being in a single contiguous grouping.  Return result modulo 1 billion and 7.

A2) Small input there are 10! orderings to try, so it can just be brute forced.  Large input there are up to 100 sequences, so something smarter is needed.

The approach I would have gone with is categorizing the inputs.  If a sequence contains just the one character some number of times, we call that a ‘repeater’ (even if it is just a single character).  If a sequence starts with some number instances of a letter, we say it starts with that letter.  Similarly for ending.  Any characters between a start and end and the sequence also gets categorized as having those characters in the middle.

From there a bunch of impossible scenarios can be detected straight off the bat.  Only 1 sequence can start with any given letter, excluding repeaters.  Same for ending.  No 2 sequences can have the same characters in the middle, unless they are both repeaters.  A single sequence can’t have the same character in the middle multiple times, unless they are contiguous.  (I note now that the problem can be somewhat simplified by removing all contiguous duplications from the input sequences.  They don’t change the problem and just complicate detecting invalid scenarios.)

Next the inputs need to be grouped.  A starter and an ender of the same letter (and the repeaters) must be in the same group.  When gathering a group, you can follow the starter to an ender, and then to a starter, and to an ender and so on, and vice-versa, to get a full group.  However if you detect a cycle, this is also impossible.

The final part is to calculate the number of orderings.  If there are n groups, those groups can be selected to form n! orderings.  Each group has only one ordering, unless it has repeaters.  Each letter in a group which has k repeaters, contributes k! orderings.  All these factorials must be multiplied together, continually using the modulo 1 billion and 7 as required and use 64 bit integers to avoid overflow.

Q3) Given a NxM grid of points connected by horizontal and vertical lines, determine the minimum number of covered points required to enclose at least K locations.  Enclosing is defined by either being covered or being unable to connect to the edge of the grid without going through a covered point.

A3) The small input has N*M less than or equal to 20.  This actually limits a lot of the possible scenarios, but it does allow a brute force of whether each point is covered or not.

The large input has N*M <= 1000, which gives a lot greater possibilities for tricky corner cases, but does easily allow for a O(N*M*(N + M)) time algorithm.  (Examples of this can be found in the solutions to download.)

So this problem has a few things to it, which makes it a bit tricky.  First of all the problem is actually slightly simpler with an infinite grid.  In this scenario we can ask ‘what is the largest area that can be enclosed with n stones and not have to worry about whether the ideal shape runs into the edge of the grid or not.  So if n = 4k, the ideal shape is a diamond pattern.  Proving this may be non-trivial in practice, but it makes a lot of sense.  In such a shape any attempt to increase the area by moving a stone, forces all the other stones to move, just moving the shape.  If n = 4k+2 the ideal shape has 2 options, either a diamond with 2 longer opposite sides, or take the 4k ideal and duplicate 2 of its vertical or horizontal extremes, kind of making a blunted diamond.

 ## 
#  #
 ##

When I was first thinking about this problem I made the mistake of not considering 4k+1 and 4k+3 correctly.  Looking at the case of k=1 led me to conclude that these could only cover one extra position in terms of area compared to the one stone less case.  Instead the ideal scenarios look more like this:

  ##
 #  #
#    #
#     #
 #   #
  # #
   #

The general pattern that can be derived is that there are 4 diagonal lines, which either meet at a point or a blunted point.  4k is a perfect diamond shape, 4k+1 one diagonal line moves outwards, causing 2 blunts.  4k+2 moves out two diagonal lines, if they are adjacent then there are 2 blunts on opposite corners, if they are opposite it is all points, but the diamond is stretched.  4k+3 moves out 3 diagonal lines, resulting in 2 adjacent blunted points on a stretched diamond.

 ##
#  #
# #
 #

So, how does this interact with the the borders?  Well it turns out that so long as you choose adjacent case for 4k+2 (or maybe even if not?), simply truncating positions to fit inside the box is as good as you can do.  I don’t have a good proof, but once the shape is too big, the edges have to be connected, so you get runs across the edge, and minimizing stones becomes cutting diagonals across the corners, which ends up being the same as squishing the ideal shape into the available space.

So the O(N*M*(N+M)) solution is to try every area less than or equal to N*M, and cut off corners incrementally until they no longer touch the edge.  Since every area is tried, the ideal scenarios that don’t touch the edges of the full NxM grid are all tried because they are all touch the sides of some size rectangle.  Taking turns cutting the corners off one by one goes through the 4k, 4k-1, 4k-2, 4k-3, 4(k-1) sequence, pushing the diagonal lines inwards incrementally.  The best solution of all tried is returned.

A more interesting (but same running time solution) is to consider ‘can it be solved in n stones’ and then depending on the modulus the formula for the 4 boundary diagonal lines can be formulated, knowing the border will be a total of n stones, the area can then be calculated by walking over every point and checking whether it is inside the 4 diagonal lines.  The solution I saw using this approach tested cutting off corners in both orientations of the NxM grid, but I suspect that with a little thought it could be reduced to only testing one of those 2 orientations.

This approach leads to an O(N+M) solution.  Since the worst case answer is 2N+2M-4, you can walk upwards towards that value checking each n until one works.  If you can calculate the covered area in O(1) time, this is O(N+M).  This is much more complicated piece of code, but by calculating the intersection points of the 4 boundary lines with each other you can calculate a reduced area (if the shape isn’t already going to touch the edges) and then the number of empty points in the corners of this possibly reduced area can be calculated by k(k+1)/2 where k is the index (counting based on 0 from the corner) of the first filled in point along the edge, which is found by calculating the intersection of the diagonal line with the edge.  The sum of these 4 corner cut-offs is subtracted from the the total area to give the number of enclosed points.  There are 4 diagonal line pair intersections and then 4 diagonal line with a edge intersection calculations involved, all of which are simple formula solves (although the blunted corners result in the ‘intersection’ of the 2 lines being somewhere between points where you can place a stone, this is easily special cased) which can be done in O(1) time, for a total of O(1) time to calculate the covered area.

Special cases.  If the desired area is 4 or less, no need to do any work, the number of stones required equals the area.  This is also the case if the minimum of N and M is <= 2, since there is no way to surround a square in such a narrow space.  Also not obvious in the discussion above is when the desired area is >= N*M-3.  In this case the amount cut off a corner is 0 in some cases, which still works, it is just a bit unexpected considering all the use of diagonals in my description.

GCJ14 R1B

Another week, another round of a coding competition… or so it seems this time of year.

Top 1000 advancing to round 2.  The cut-off was fully solving question 2 and the small input of question 1 in 2:20.  All small inputs in a fast time came 1035th, which was interestingly close to advancing.  The score to get by without having to worry about time was achieved by all small inputs and the large of the first question.

Q1) Given a game where characters can be duplicated and consecutive duplicate characters can be collapsed back to a single character, determine if a set of strings can be made equal, and if so, the minimum number of moves to achieve this goal.

A1) Determining whether the goal can be achieved is easy, just replace all sequences of consecutive repeats with a single character, and determine if all the input strings are equal.  This produces a ‘canonical’ form that all must match.  For the minimum number of moves it isn’t really that much harder.  For the small input there are only 2 strings, so choose one of the strings and count how many moves it takes to make the other equal to it.  There is no advantage to trying to meet in the middle, so on a per canonical string character basis just take the absolute difference of the counts in each of the strings and sum them all for the result.

For the large input there are up to 100 strings, but because each string is only at most 100 characters, no letter can be repeated more than 100 times.  Therefore the ideal to converge upon will be no more than 100 repeats and no less than 1.  Since each character of the canonical form can be considered independently, all possibilities can be easily tested within the time limit.  Just try each value of 1 to 100 inclusive and calculate the sum of absolute differences, the smallest sum is the minimum number of moves for that canonical character.  Then just sum all of those minimums for the final result.

The most challenging part of this question is probably that the large input is considerably more ‘different’ to the small input, so a bug in the code could easily get past the small input only to fail on the large input, even though it would easily run in time.

For interest, an extension of this question where the maximum length of a given string is unbounded by the maximum length of the canonical form is short, the problem is still easily solved so long as the counts are provided rather than having to parse the unbounded length string.  The sum of a set of absolute difference functions all with equal slope has a minimum at the middle of the sorted list of 0 points of the individual functions.  For an even number of functions, either of the middle 2 will work because the the sum is flat in between.  Thus with a value selected the total of absolute differences can be calculated for a total run time proportional only to the number of strings and the number of characters in the canonical form.

Q2) How many pairs of 2 numbers selected from specific ranges when bitwise ‘and’d together have a result in a 3rd specific range.  All ranges start from 0.

A2) Easiest small input ever?  With range sizes of only at most 1000 each, nested loop over the input ranges and calculate the and and compare to the maximum value of the 3rd range.  If less increase counter.  Once loops are done, return counter.  6 trivial lines?

Large input however is much more challenging.  With ranges of up to 1 billion, such brute force is out of the question.  Given the bitwise and function and the large ranges, running time seems like it will need to be related to the logarithm of the input range maximums.

Determining whether 2 numbers ‘and’ to give a 3rd is trivial, which leads to the thought, is there some way to convert the ranges into batches which can be easily processed in parallel.  Perhaps using a ternary notation of 0, 1 and ‘don’t care’?

All numbers less than n can be grouped in a ternary notation by having a prefix which is equal to the start of n, followed by a 0 where there is a 1 in n, followed by ‘don’t care’ for all subsequent bits.  The number of such patterns needed is equal to the number of set bits in n, which is worst case proportional to the logarithm of n.

So the problem now boils down to, generate these ternary representations for the 2 input ranges and the output range, then for every way of selecting from those 3 lists, determine how many results it allows.  Sum those all up, and return.

So the core part of that is ‘determine how many results it allows’.  So inside the triple nested loop to select the 3 ternary representations being tested another loop over each bit is needed.  Each bit is independent in the bitwise and, so the number of ways of doing each bit can be considered a multiplier.  Multiplying them all together will give the number of solutions that are derived from this particular selection.

Handling each bit can be done with a large if statement selecting on the 3 different values which can be in each of the 2 inputs and the output.  For example ‘don’t care’, ‘don’t care’, ‘don’t care’ has 4 possible scenarios and ‘don’t care’, ‘don’t care’, 0 has 3 possible scenarios.  Such an if statement is quite large and convoluted, so some more nested loops might be simpler.  Loop over each possibility for the 2 inputs, skipping if it doesn’t match the actual input values.  Then calculate the and of the 2 input bits and compare it to the expected output value incrementing the counter if it matches.  The simple match function for ‘bit vs ternary value’ is handled much more easily than the huge if statement.

One catch is that the 3 ranges originally specified 2 are exclusive and one is inclusive of the maximum, so applying the above approach the inclusive bound needs to be incremented to make it an exclusive bound first.  Alternatively all numbers less or equal to n can be considered by using the same set of patterns above and adding the exact pattern of n as well.  This is equivalent to setting the bit one past the end of the number to 0 and so can be treated as one extra loop, with appropriate special case handling.

Q3) Given a set of locations with distinct ‘values’ and a list of connections between locations (all locations are connected), determine a order of visiting which doesn’t arrive at any node more than once, but can backtrack as much as you like, which visits every node such that the sequence of first visits is ‘smallest’.  Smallest is defined by comparing the first elements of two sequences, then the second elements, and so on until one is smaller than the other.

A3)  The small input only had 8 nodes, so brute force was plausible.  Just had to make sure you don’t infinite loop.

First thing to realise is that the values of the locations do not matter, so first step is to renumber the graph so that the smallest value is node 0, the second smallest is node 1, and so on.  Then the walk of the graph desired is one which prefers smaller node indexes.

The solution seems to be a greedy approach.  First thing to realise is that node 0 (in the renumbered scenario) is always the correct place to start – since you can start from anywhere, and it is always possible to walk to every node of a connected graph, starting from any given node even with the additional restrictions specified by the problem.

From that first location you will always walk to the smallest index connected to node 0.  But after that it becomes slightly more tricky.  More generally the problem becomes deciding what to do when you have visited a set of nodes V and have a current path from node 0 to current node N.  For each of the nodes on the path, you can go to any of the nodes not in V that are connected to the node in the path.  This leads to a set of scenarios which need to be prioritized for the greedy approach.  The obvious priority is the index of the destination.  It is always preferable to go to a smaller index if possible.  However we need to break ties as the graph can be fully connected, in which case it is possible to reach every node from every node.  Here the idea is that deeper in the path is better.  Every time you backtrack you lose the possibility of going from that node in future, so it is kind of an opportunity cost.

So that seems pretty simple – but just choosing the best value from the set of scenarios, doesn’t always work.  The problem is that it may cause you to back track to take a link to a low index node, but there might be nodes that can only be reached through the node you backtracked from, and there is no way to get back to that node any more.  Therefore before taking any option in the priority order of options, you must perform a reachability check. A recursive walk from each node in the potential future path, out in to the world not visiting anywhere already visited, do you visit every node not already visited?  If the reachability check fails, that option must be discarded and the next highest priority considered.

The running time of this algorithm is a bit complicated, but an easy upper bound can be found.  There are O(N) steps.  Worst of these steps has O(N) nodes on the current path.  This results in O(N^2) possible scenarios to consider.  Each of which could require a reachability analysis which takes O(E) or O(N^2).  Thus an easy upper bound is O(N^5) which with 50 nodes is fine.  In practice the cases with high number of scenarios at a given step also tend to pass reachability easily, so the code runs within time with ease.

TCO14 R1C

So, again I didn’t need to compete, which was good because my weekend was already very very busy…  2 easy questions this week, with 750th place scoring almost 628 points.  This time about 230 people got all 3 problems out in time.

Q1) Remove duplicate letters from a string, leaving the first instance.

A1) Even with the length limit of up to 1000 letters only the really silly O(N^3) solution  would run too slow.  Simple boolean array for seen and add characters to a list if they haven’t been seen yet then convert character list to string for return.  O(N) – very simple.

Q2) Count the number of multiples of 3 (but not 15) and 5 (but not 15) and 15 in a large range.

A2) There is always 1 multiple of 15 and 4 of 3 but not 15 and 2 of 5 but not 15 in the range n=15k to n=15k+14.  Thus take the modulus of the start and end in order to move them up and down to a 15k and 15m-1 value respectively, then hand code the start/end sections and add the results to m-k, 4(m-k) and 2(m-k).

Q3) Given a random walk of length n, what is the expectation value for the number of distinct locations it steps on, including the starting location.

A3) So with n up to 500, there are 2^500 different random walks possible, so brute force isn’t going to work.  The best of my initial guesses was to do this as a recursion on the average width after k more steps with current location x and a to b already painted, averaging on either the walk left or right.  This gives O(N^4) which is too slow and way too much memory, but the actual location x doesn’t matter, instead a and b can be normalized as distance to left edge and distance to right edge.  This gives O(N^3), which is feasible for run time, but the memoization table would exceed memory limits.  Even using some symmetry like if distance to left is greater than right we can flip them without loss of generality, I am pretty sure memory usage cannot be made to fit.

But this does give me another idea.  If we could get rid of one of left and right, O(N^2) space would be easy to fit.  This leads to the idea that memoize over remaining steps and current width where the current location is at one edge.  In this scenario the recursion would have 2 possibilities.  50% chance of growing width by one and reducing remaining step count by 1 and some other chance of not changing the width, but reducing the step count by k.  This requires the computation of the number of ways to choose a random walk which stays strictly in a given width starting from the edge of that use up all remaining steps, or lesser steps which end up at one of the edges of the width.  Using precomputed combinatorics tables, I think this can be calculated in O(N) time, giving the result needed, but the details are more complicated than I think I could have worked out in competition.

Turns out that I really got lost on this one, because there is a much simpler solution if I had of stuck on back where I started.  The simple O(N^3) space and time solution can be made to work by using a dynamic programming approach instead of recursion with memoization.  In this case you can use two O(N^2) size arrays and switch between them to calculate each number of steps remaining, since the recursion always decrements steps remaining by exactly one.  The final generated table contains the answer needed.

GCJ14 R1A

Looks like it was pretty competitive to qualify in this round.  With two questions that were not that difficult and one which was quite unusual and challenging the top 1000 were defined by getting both of the first 2 questions completely in under 2:15.  The third question was unusual because it was based on randomness to the point that there was a chance you would get it wrong even if your code was as good as it could be.

Q1) Given a set (no duplicates) of binary representations of equal length, determine if there exists a bit flip pattern which can be applied to all of them such that they become set equal to a second set (no duplicates) of binary representations of same size and length.

A1) The actual wording of this question was quite involved and I suspect would hide how easy this question is to many.  The small input was limited to up to 10 bits in length, so a brute force of the 2^10 bit flip patterns could be tried.  Large input however went to 40 bits to ensure such an approach wouldn’t work.

Looking at the other constraint however, the number of elements in each set, that is limited to 150, so a O(N^2) is clearly feasible.  Since every element in the original set has to map to an element of the target set, obviously at least one element in the original has to map to an element of the target set.  So take one element of the original set and determine the bit flip pattern which will work for each member of the target set.  Then for each of these up to 150 patterns (rather than potentially 2^40) the set equality can be tested.

For a nice compact solution code, the bit patterns on input can be parsed into 64 bit integers.  Then the bit flip pattern can be represented as another 64 bit integer, which will be applied using xor.  In this version generating the (up to) 150 patterns is as simple as calculating the xor of one element of the first set with each element of the second.  Each element of the target set can be placed into a set object, and the set equality (since we already know that they have the same number of elements and that xor with a pattern is a bijection, so there won’t be any duplicates generated) is as simple as checking that every value in the original set xor’d with the pattern value is a member of that target set.

Q2) Given a connected acyclic undirected graph, determine the minimum number of nodes to remove to be able to make it into full binary tree (where every node other than a leaf has 2 children for some choice of root).

A2)  Again the small input invites brute force with only up to 15 nodes.  The large input of up to 1000 nodes is more interesting.

With a limit of 1000 nodes O(N^2) is acceptable, and this is not too difficult to obtain.  Choosing each of the nodes in turn as a possible root node, a recursive solution taking two parameters, current node index and previous, can traverse the tree in O(N) time.  Specifically the recursion is the maximum nodes which can be kept – the final answer then being the best of each possible root subtracted away from the total number of nodes in the original graph.

The recursion is to consider every edge of the current node which doesn’t connect to the previous node.  Recursion down those edges will return a set of values.  If the set is 2 or greater, choose the largest two and return the sum, plus one for the current node.  Otherwise just return 1, since only this node can be kept.  For each recursion run from a root this obviously linear in the number of edges. Each edge is only considered twice, once on the way out and again to be excluded at the next level down in the recursion.  Since there are no cycles in the graph, the recursion trivially terminates when it gets to a node with no connections other than to the previous node.  Since the graph is acyclic and connected the number of edges equals N – 1 and so the total running time over all recursions is O(N^2).

Of interest to me here was whether the problem can be solved in O(N). The recursion itself invites a memoization over previous and current, which has O(N) states, but a simple star type graph, shows the total time in this case is still O(N^2) because the centre node of the star can be arrived from each of the leaves and has O(N) exiting edges which must be considered.  Even with the trivial optimization of not calling the recursion if there is only 1 candidate which fixes the basic star case, slightly more complicated star type graphs are still O(N^2).

I think I have a solution, which unfortunately complicates the algorithm a bit.  The idea is for each node to gather the (up to) 3 highest values of the recursion which have it as the previous node.  So we start by performing the standard set of recursions, but rather than simply taking the best 2, and memoizing the sum we take a slightly different set of rules.  First check if a memoization value for current node exists.  If not, recurse as normal, but store the 3 highest values and the previous index in the memoization for the current node, rather than memoizing against the sum against the current/previous pair.  If a value does exist in the memoization table but the table entry also contains a valid previous index which is not the same as the current one, call the recursion specifically for that edge, and replace the smallest value in the memoization table if appropriate, and mark the previous index in the memoization table as invalid.  Now there are up to 3 largest values in the table, and we can select the 2 largest of those are not for the previous index.

So, the memoization table contains O(N) entries, each of which get updated at most twice.  Since it is clear that the recursive calls made are only a subset of those made by the original algorithm, it will terminate.  The claim that it is has a total run time of O(N) is slightly trickier.  The first time a specific value in the memoization table is calculated, it could involve O(N) calls to recursion, but because the memoization is stored keyed by nodes instead of by previous/current pairs it won’t potentially be rerun for multiple different previous nodes, which is what caused the O(N^2) logic in the previous version. Basically each node may be visited once for every edge it has, but the total work performed over those visits is proportional to the number of edges it has.  Since each edge connects 2 nodes, each edge in the graph might be considered twice, but not repeatedly as in the original algorithm.  This makes the total run time proportional to the number of edges and hence total run time is O(N).

Q3) Given 120 examples of permutations of the numbers 0 through 999 where each one was generated either by a correct, or incorrect random shuffle with a 50% chance, correctly identify at least 109 of them.  Initial state before shuffle is ascending order and the incorrect random shuffle performs one random swap for each index in order from first index to last, with a randomly selected index which is selected amongst all indexes. (As opposed to correct which only selects from current or higher indexes.)

A3) Since the incorrect shuffle has as a subset of its possible swaps all of the possible swaps performed by the correct shuffle, there is no pattern which is ‘obviously’ only the output of the correct shuffle.  Also since every permutation is a possible output of the correct shuffle there are none which are obviously only the output of the incorrect shuffle.  Therefore the best you can do is come up with a heuristic which tells you something about the probability of a permutation being from the incorrect shuffle and hope that it can managed to identify 109 out of 120 a decent percentage of the time.

I think the key component of this problem is actually writing a program to perform the incorrect shuffles so you can look at the probability distributions.  One aspect of a correct random shuffle is the probability of index i being k should be equal for all valid k and i.  If you were to run 1 million shuffles for instance, there should be approximately 1000 cases of any given index i being equal to a specific value k.  Running 1 million incorrect shuffles, takes a while, but not so long as to rule out doing it once for every time you submit the problem set.  Indeed this naturally leads to an approach of generating this table, and then for each input looking up the index and value for the 1000 elements, and calculating a product of the ratio of the value in the table to 1000 (to avoid over/underflowing double) for each lookup.  The higher the result the more likely it is from the incorrect shuffle.  It might seem at first glance that a threshold of one is appropriate but the calculations are more complicated than that and they are effectively overlapping distributions where you want to choose a threshold to minimize false positives and false negatives. Potentially better luck can be had by taking into account the fact that things are generated with equal probability of correct or wrong and instead sorting the scores and giving out the highest half scores as being incorrect and the lower half as being from the correct sort method.

I found this approach to be somewhat cumbersome, and 1 million incorrect shuffles is 1 billion random number and swap operations, which is a lot of CPU time to be chewing up every submission.  So I had a look at the generated tables and tried to come up with a formula which approximated their outcome with minimal computation cost.  I came up with if value <= index return 1.0, otherwise linearly interpolate 1.3 to 0.7 for values between index + 1 and 999.  Using this and some sample testing I ran on my machine with a bunch of randomly generated problem sets I was managing to get a >93% success rate at identifying individual permutations correctly with an appropriately selected threshold.  This may only be 112 out of 120, but it does provide a pretty decent chance of getting past on any given submission.

TCO14 R1B

Since I got through last round, I didn’t have to stay up all night this weekend – which was good since I was pretty tired already!

43 people solved all 3 problems – >300 2 and the 750th cut off was 196 points, so at first glance the problems look like they might have been more difficult than last week.  The number of people with a point score in the >190 range was almost 1400 – so a small time difference on the first problem was a big difference in placement.  At first glance I thought 196 points was a pretty low score, but it turns out Q1 was only worth 200 points this time, so it truly was a race to qualify if you were unable to solve either of the larger questions.

Q1) Given a sequence of ‘good’ and ‘bad’ and a value for each, determine whether the running sum is ever negative (‘bad’ value is subtracted, ‘good’ is added).

A1)  Such a very very straightforward question here, I can see where the rush came from. 4 trivial lines of code will get this done…

Q2) Given a rectangular grid of cells, determine the minimum number of horizontal or vertical lines (which extend the full width/height each) which will ensure all ‘wolf’ containing cells are separated from all ‘sheep’ containing cells.

A2) A big step up in difficulty here, reflected by the 600 point score I guess.

A simple starting point is every wolf adjacent to a sheep implies a line.  With a 16×16 grid brute force is just out of reach with there being up to 30 lines.  However the limit of only 16 is suggestive when if the problem was was an easy polynomial DP type solution it would probably allow up to 50×50 grids.

So, one approach is to try all 2^15 options for one dimension and then ‘as required’ add lines in the other dimension.  This ‘greedy’ approach of putting off adding lines as long as possible, seems reasonable when there is only one dimension of consideration.  Alternatively if that seems too risky there should be a simple DP of minimum number of lines needed for the first k columns with the last line at column x, however the running time is starting to get a bit risky at 2^15 * 16^3 (requiring some pre-computing to get it down to that low even I think…).  The as need approach (looking at someone else’s solution which passed) seems like it can be done in 2^15 * 16^2 operations which is much safer, this still requires some care to avoid accidentally getting an extra factor of 16.

Q3) Given a tree of n nodes where each node other than the first specifies its parent, determine probability that the kth ‘bird’ can land on the tree if they random walk down from the root to avoid having more than 1 birds on a node and fly away if the final child they arrive at is occupied.

A3) So my first thoughts about this problem was that it was a simple ‘calculate probability of kth bird landing on each spot using probability that each spot is filled as of k -1th bird’, and accumulating up from the no birds scenario, but this doesn’t work – because of correlation effects, there are cases a simple method like this will count that can’t actually happen.  The probability of landing depends strongly on the number of birds that have passed through a given child.

Instead you need to calculate the probability the kth bird will land if k-1 birds have already passed through a given node.  Boundary conditions are If k = 1, then 100 %.  If the node has no children, then 0%.  Otherwise binomial coefficients are involved for each child and the subset of k – 1 which went down that child, plus 1 for the current bird.  Basically given the c children, what is the probability of n out of k – 1 birds go down any specific child is multiplied by the chance n+1th bird will land if n birds have already gone through that child.  This is then averaged over the children.

I suspect I wouldn’t have solved this problem in time, the strong dependence in the probability on the number of birds is a bit deeper into probability theory than I grasp intuitively.

GCJ14 Qualification Round

So, I didn’t compete in this, but plenty of people did.

20595 people qualified with the minimum 25 out of 90 points. 1737 people got 90 out of 90. ~25500 people submitted at least one solution.

Q1) Given 16 distinct numbers arranged in a 4×4 grid, and a selected row, and the numbers after a reshuffle and another selected row that was picked, determine either the one number in common, or return ‘Bad magician’ if there is more than 1, or ‘Bad volunteer’ if there is less than one.

A1) I remember learning this magic trick as a child, although it was in the context of a deck of playing cards rather than 16 numbers.  But that seems neither here nor there – the code looks like it should be pretty simple either way.  You don’t even have to parse the 4×4 grids into 2 dimensional arrays, since the input gives you the selected row before the grid, so you can just only read in the one row which matters.  Then given the 2 4 element arrays, you can calculate the result using a pretty straight forward nested loop – or you can be lazy and just add the arrays to standard Set data types and ask it for the intersection.

Q2) If you have a base earn rate of 2 cookies per second, and buying a cookie farm costs C  cookies and increases your earn rate by F cookies per second, and have a target of X cookies, what is the shortest possible time to get to your target.  Earn rates and buy times are not based on integer time, can be any real number.

A2) So my first instinct looking at this question was pretty poor – I saw finding a minimum on a function which seems to fairly obviously only have a single turning point in the range of positive numbers of cookie farms bought.  This naturally made me think ternary search.  Which would work, but is completely pointless – because calculating the time to win for a large number of farms does almost all the same work as calculating the time to win for every smaller number of farms, so performing a ternary search is just a waste.  Also the upper bound of the ternary search needs to be defined, which is not trivially obvious.

So the simple solution is to have a running total of how long it takes to build n farms, and for each one, compute how much additional time to get to the target.  Once the total starts getting worse, just return the best found.  The running time of this is O(B) B is the number of farms which need to be built.

The size of B is an interesting question itself.  We can determine when the turning point is going to be by an equilibrium equation.  X/(BF+2) = C/(BF + 2) + X/(BF+ 2 + F)  For B smaller than this, it is faster to build another one, and for B greater, it is slower (for constraints of X/C/F all being positive at least…)

Solving for B:

X (BF + 2 + F - BF - 2)/(BF + 2 + F) = C
BF + 2 + F = XF/C
B = X/C - 1 - 2/F

Interesting here is how insignificant F is.  This is because a high F lets you build everything quicker, not just the final target, so if the starting rate had of been F rather than 2, it would have been completely irrelevant except as a scaling factor of the total time.

Since C is >= 1 in the problem statement, O(B) is practically O(X).

However, now that we have exact number of farms to build, interest turns to whether the solution can be done in O(1).  This becomes a problem of evaluating the sum of 1/(ax+b) for x = 0 to x = B.  This seemed vaguely related to the Harmonic sequence sum (a=1, b=1), which I provide a reasonably fast implementation in TMD.Algo, so I suspected it was possible.  Wolfram alpha told me that it was (digamma(b/a + B + 1) – digamma(b/a))/a.  So if you happen to be programming in a language which provides digamma evaluation, you are set.  If not, you will need to write it yourself.  Wolfram alpha provides formulas for small and large X, the large X one bearing a striking resemblance to the Harmonic sequence sum I implemented for TMD.Algo.  The trick here is that digamma(b/a) is digamma(2/F) which can easily be in the range of 1 to 2 which is probably its most tricky area to approximate.  This is especially difficult because the digamma passes through 0 in this vicinity, so the recurrence relation digamma(x+1) = 1/x + digamma(x) is going to end up subtracting very similar numbers, resulting in significant relative error.  But since that is only near 0, and if small B is handled separately the digamma(b/a + B + 1) term should be dominant enough to swamp the error.  So yes, O(1) is plausible, if you are happy considering digamma as O(1) when its implementation will probably involve at least 5th order approximation functions to get sufficient places to fill a double precision float.

Q3) Given an RxC board and M mines, determine if they can be placed such there exists a location where a single click reveals all non-mine squares, using classic minesweeper rules where 0’s auto expand in a cascading fashion.

A3) This problem by far had the lowest success rates.  Which is interesting because the a maximum of 5×5 it is actually plausible to brute force the small input.  Try each of the 2^25 patterns, throw away the ones which don’t have M mines (to avoid implementing a combination generator), then for each of the rest, calculate the numbers and ensure all numbers are adjacent a 0 and all 0’s are connected (flood fill count vs total 0 count for instance), return the first one you find, with any one of the O’s marked as the click location.  This is  worst case >125million instructions, which is quite a few given the 200 test cases, but given the memory locality it should be plausible for any language without excessive overhead.

More interesting is doing the problem without brute force, as is needed for the large input.  This isn’t hugely difficult, but the number of corner cases is significant.  It is convenient to write a transpose function so you only have to deal with the cases of R >= C. (And remember to transpose back if needed when you are done.)

From there there are several cases which need special handling.

  • M = RC – 1.  Here the solution is, fill in everything and overwrite one of the mines with the click location.  Handling this scenario first is useful.
  • C = 1.  Fill all the rows until you reach M, and then put click location in the last row (or anywhere not next to the mines).
  • C = 2.  Here M must be even and RC-M must be at least 4.  If those are true, then simply fill every row until you have M mines, and place click location in the last row (or again anywhere not next to the mines).

Which leaves the general case C >= 3.  C = 3 itself has some specialness but it can be treated under a single code path with the rest easily enough as far as I can tell.  For convenience, define L as RC-M – the left overs.  If leftovers (L) is >= 3C it is almost as simple as just filling from the top, left to right.  The one case which needs handling is L % C = 1, in this case the one left over on the right is not adjacent to a 0.  This can be solved by moving the mine next to it, down one row and all the way to the left.  This newly placed mine is fine so long as it is on the 3rd last row or earlier, hence the condition of >= 3C.  In fact we can extend this condition to L >= 2C + 2, since the first problematic scenario is L = 2C + 1.

In fact, L=2C is trivial, so our first step is to fill everything except the last 2 rows.  If L = 2C+ 1, we remove 3 from the 3rd last row, and put 1 in each of the first column of the last 2 rows. This pattern of switching 3 for 2 is one of the basic steps we can use from here on. However, this only makes sense if C >= 4 which brings us to how this code path can be made to work with C = 3.

So, what values of L are clearly impossible?  L=1 is fine, we covered it first up. L=2 is bad, there is no way that only 2 spots can exist with either of them being a 0 (excluding C = 1 as all of this discussion here will). L=3, same.  L=4, this is possible in a corner so long as C >= 2.  L=5, again no way.  L=6 works with an edge which allows the 3×2 empty rectangle. L=7, again impossible.  L=8 has two options 4×2 on an edge or a 3×3 in a corner with the opposite corner taken out of it.  So why is this important?  Because if C = 3, L = 2C + 1 = 7, which is a known impossibility, so we can test for known impossible values of L before  starting the general case logic.

So, this leaves handling of L < 2C and L != 2, 3, 5 or 7.  Starting from having filled in all but the last 2 rows, the first operation is remove 3 from row 3 and add 2 each to the last 2 rows.  This gives 2C – 1, although it only is in a valid state if C is at least 5, but again C = 3 this is 5 and C = 4 this is 7, both of which have been excluded.  Now put the 3 back in the 3rd row, and remove the right most one for each of the last 2 rows.  This moves us to 2C – 2, and this time it is valid for C >= 2, which is all those under consideration.  Repeating the first step gets to 2C – 3, but this time only valid for C is at least 6, but again smaller C values are excluded by the L being 3 or 5 or 7.  These steps can continue to be alternated, decreasing L by 1 until the desired target is reached.  In every case the click target is the bottom right corner.

Q4) There are 2N distinct numbers selected from 0-1 exclusive, half the numbers are given to player 1 and half to player 2.  A turn consists of player 1 announcing a number, player 2 selecting a number, and player 1 winning a point if their number is actually greater than player 2’s.  Player 1 is it a disadvantage obviously, so they are considering cheating.  They would cheat by first looking at all of player 2’s numbers at the start of each game, and then potentially lying when announcing the number, knowing that in this specific game the number comparison is confidential, only the result is public, not the specific values that went in, and that player 2 will be playing to maximise their score.

A4) This questions problem statement is a bit of a mouthful, you would be better to just go and read the original…

So this problem is interesting, in that what I think is the hardest part of this problem is also the easiest, and that is ‘what is the ideal strategy’ for the non-cheating version.  Its the hardest in that I can’t come up with a viable proof in the time I allotted to considering it, but it is seemingly pretty easy to guess.  So first off player 2’s strategy is pretty obvious.  If they can win, they will play the smallest number which wins, if they can’t win, they will play their smallest number remaining.  Player 1’s strategy, I don’t know, does it even matter?  Just playing them in sorted order seems reasonable.

Speaking of sorted order, sorting all the numbers for each player is a good starting point.  For calculating the non cheating version consider each of player 1’s numbers in order, and walk up player 2’s as needed. Once you find a player 2 value greater than player 1, player 1 loses that point and increment both indexes.  Once you run out of player 2 values, player 1 wins all remaining plays.

For the cheating version first we need to identify how you can cheat.  There are 2 ways, one which is more obvious to me than the other.  The first is that you can increase the number you announce until it is just smaller than the opponents greatest number, to force them to beat you using their largest value, rather than potentially a smaller value.  The second is that if you can beat their smallest value you can announce a value of 1 minus epsilon (since the values are all 0 to 1 exclusive, you can always announce a value higher than their actual highest value) and your opponent then plays their smallest value, which you beat, just as you stated you would.  They are both important, but this second one has the most obvious consequences.

To calculate the cheating win, take the sorted lists and compare the first values.  If player 1 is greater than player 2, player 1 wins this round and both indexes advance (by using cheat method 2).  Otherwise player 2 wins this round, but by using cheat method 1, player 2’s largest value is the one which will be discarded, so only player 1’s index advances.  Repeat until all player 1 numbers have been considered.

TCO14 R1A

So the reschedule actually happened this time.  For a moment I suspected it might not as my client broke at room assignment time, had to log out and in again.  I have been sick for the last 4 days, so I wasn’t looking forward to even more 4am nights if this one had been cancelled as well.

750 to advance… Turned out to be pretty easy looking questions with 750th before challenge phase being 500points, almost 200 people submitted something for all 3 problems, including myself.  Been a long time since I submitted a 1000pt problem, although this one did seem pretty easy…

Challenge phase had a few successes, but not a huge number.  750th shifted slightly to ~495 points, and the number of people with all 3 still submitted was almost 140.

System testing took out another chunk, but I survived unscathed.  59th.  Even assuming all 250 byes would have beaten me, that is a decent placement…  Just over 100 submitted all 3 problems successfully.  750th place dropped down a tiny bit to 484 points.

Ratings took forever due to a glitch with the display of the results (seems the results were not affected much at a basic glance at least…) but I am now 1882 – which is a new personal best.

Q1) Given the ability to sort L characters and discard any characters after those sorted, return the lexicographically smallest result given an input string and any number of uses of this ability.

A1) So, I was a bit slow here, spent too long trying to decide whether the greedy approach was right.  Turns out it was.   So sorting the last L characters then moving one character forward, repeat until getting to the front, carries as many alphabetically early characters forwards as is possible, and since shorter strings compare earlier, truncating the rest is always a win.

Q2) Given a string S and the ability to permute it such that the index of a character doesn’t move by more than D, determine the lexicographically smallest result possible.

A2)  Again greedy, but this time I was more confident.  Greedy approach is to take the smallest character, at the earliest index available, to use as the new letter.  Exception being if there is a letter that is still unused which is D characters earlier than the current index, in which case it can’t be put off any longer, and must be used now.  I used a simple search the 2*D+1 locations for each output index, but I saw one solution which was O(N log D) rather than O(ND) by using a sorted set for the letters under consideration.

Q3) Given a set of lamps controlled by switches, which might also potentially toggle one or both of their neighbours, but always toggle their target lamp, determine the worst case minimum number of lamps that must be on.  Lamps are in a line, not circular.

A3) So, the description of this question is a bit awkward, but the examples provided were surprisingly useful.  They clearly showed than a sequence of YN(on off) or NY(off on) had a worst case of 1 light on.  A sequence of NN or YY had a worst case of 0.  So a greedy approach of trying to get as many YN, NY pairings as possible seemed sensible.  Except the final example didn’t work.

I took longer to get it than most, but I did get there in the end.  The missing case is ‘YYY’, This can be wired up such that at least one of the lights is always on.  Seemingly a greedy approach may still work from here if written carefully, but at this point I decided enough was enough with the greedy solutions, so I wrote a basic DP where the best ‘worst case’ for the first n lamps was either that of n-1 lamps, or n-2 lamps + 1 if NY or YN or n-3 lamps + 1 if YYY.

The fact that there are no other scenarios which need considering is not exactly obvious. But I did prove to myself that YYYY worst case is 1 and YYYYY is also worst case of 1, so I was pretty confident.

GCJ 2014 Qualification Has Started

Registration is open until the qualification round is finished, but you will probably want to leave yourself some time to actually do the problems, so I suggest taking a look sooner rather than later…

TCO14 R1A failed to happen last weekend, so it was rescheduled for tonight – I’m not sure what the registration time requirements are for TCO this year, but it might not be too late…

Google Code Jam 2014 and Top Coder Open 2014

Just a quick note – GCJ14 registration is now open.  Qualification round start on April 11th (in some time zones at least…) and registration is open until any time before the round ends (round is available for 25 hours).

Google is still employing me so I won’t be able to compete again this year, but the TCO algorithm schedule has finally been announced so I now know my sleepless nights in April won’t only be because of my baby daughter…  Yes, yet again every round is at 2am Sydney time.  Interestingly it seems GCJ and TCO schedules were not quite so neatly organized this time around, there are a couple of occasions where they are scheduled on the same day.  Although one of those is the qualification round for GCJ, so people should be able to schedule around that pretty easily and the other is at least 15 hours between start times.