GCJ15 R2

The all important t-shirt round, top 500 advance but 1000 t-shirts were on offer.  Four problems for round 2, in the end advancing meant solving all the small inputs and the easiest large input, or one less small in a very fast time.  Even with a slow time that was enough to get the t-shirt, 2 small and a large was okay if you were fast, or the second small was for the hardest problem.  I think I stood a decent chance of advancing if I had of been competing this year, definitely would have gotten the t-shirt…

Q1) Given a 2D grid where some cells propel you in a direction, what is the minimum number of cell with direction that need to be changed to ensure that regardless of starting location, you never fall off the edge?  (Note its not always possible to solve, so return ‘impossible’ if there is no solution.)

A1) I didn’t immediately get this problem, but when I looked at it again I realized it was trivial.  Seems the contestants agreed, with very high success ratio and high solving rate.

In the end it boils down to the question, why would you fall off the edge at all?  Because a cell is pointing there with nothing else in the way.  So for each such cell, just point it at another cell with a direction.  If you can’t then the problem is impossible and you should return as such.  Otherwise you are done, just count how many cells you had to change.  The run time of the simple brute force is obviously no more than O(WH*max(W, H)) which given the constraints is trivial.  A tighter bound is actually O(WH) every cell can only be traversed at most 5 times, once to find the cells with direction, and then 4 times from being reached from each direction by other cells. So even if the problem had of allowed much larger grids, it would still have performed in time.

Q2) Determine the minimal time to fill a container of volume V to temperature X, given a bunch of sources of flow rate F_i and temperature T_i.

A2)  The small input here has only 2 flow sources, and the problem gives the formula for mixing temperatures.  Solve simple simultaneous equations in 2 variables and you are done.  The inputs are given to 4 decimal places, and the answer is 1 part in 10^6 accuracy, so doubles sound like they would be fine here.  But this isn’t quite like other such floating point problems.  This one has the ‘can’t be done’ answer, if both temperatures are the same side of the actual temperature.  So you need to be sure that you don’t introduce a numerical error which means that in the case where one of the input temperatures is equal to the desired temperature, you incorrectly return impossible.  One solution is to ignore the fact that the question specifies things as 4dp, and just multiply everything by 10000.  These factors of 10000 cancel out in determining the time, so you can switch to mostly integer arithmetic, just converting back to floating point for the final division.   You have to be careful with overflows though.

The large input is too large to brute force, which suggests a greedy approach.  My best guess was to order them by temperature.  I took a look at some passing solutions and one of them did exactly this.  Order by temperature, then slowly prune either the hottest or coldest until you switch from the output being too hot to being too cold or vice versa.  Then its like solving the small input, just with one high flow mixed temperature of everything, and the last temperature you removed.  I found it interesting that they didn’t bother to solve the simultaneous equations though, they just binary searched for the flow rate that produced the right output temperature. (Since you can simulate lower flow rates just be having that tap turned off for part of the time.)

Another solution I saw instead binary searched over ‘can it be solved by time x’.  They also sorted by temperature, but instead of pruning backwards one by one, they built outwards from the middle.  Given the time t, they had actual volumes rather than rates, and could determine if each volume from a given tap could be mixed with some from another to create the right temperature.  As they used up each tap, they moved to the next one out.  Eventually all of the colder than target taps or all of the hotter than target taps get used up, and the the rest is discarded.  If the total used volume is greater than or equal to target, success and binary search a smaller time, otherwise a longer time is needed.

Q3) Given a set of sentences which are in ‘English’ or ‘French’ determine the minimum number of words that must be in common between English and French.  The first two sentences are labelled, the rest are unknown.

A3) The small input looks like a trivial brute force, but unless you start by replacing strings with numbers, the hash/equality cost is quite high and will probably stop your code from running in time.  So relabel everything to numbers.   Given the constraints there will be no more than a few hundred distinct words which you can number consecutively 0 to N, so rather than hash lookups you can just used indexed arrays.  Enumerate the 2^18 scenarios adding to the counts of times you’ve seen each ‘word’ against either English or French arrays.  Then sweep them both looking for anything which has both positive for the same index.

The large input however had me stumped.  My best guess was some kind of maximal matching or flow graph, but I couldn’t see how to construct it.  Looking at other solutions it appears max flow graph was the answer.

Each sentence has two nodes, each distinct word has two nodes.  Unlimited flow from the first to second nodes for each sentence, but only unit flow between the first and second nodes for each distinct word.  Then connect the second node of each sentence to the first node of each distinct word in the sentence, and vice versa – both with unlimited flow.  The answer is then the maximum flow from the first node of the first sentence, to the second node of the second sentence.

I don’t understand why this works though, maximum flow for a minimal answer.  Even the meaning of each node doesn’t seem obvious.  The two nodes per sentence seems redundant since there is only unlimited flows involved, probably just a convenience rather than having source and sink nodes for the first two sentences especially.  A flow between the two nodes for a word means they must be both English and French.

Looking at the case of just the input two sentences, every word in common allows a flow which goes first node of first sentence, second node of first sentence, first node of word, second node of word, first node of second sentence, second node of second sentence.  Words not in common don’t allow flows because they just form a cycle with their parent sentence.  Next simplest example, one word third sentence which is in common with English (or French) but not both.  It gets ignored.  Good.  Three word sentence with two in common with English and one in common with French.  One more flow, from first sentence through the fact its in common with English, to third sentence, then over to French by the one in common with that.  The second potential flow gets stuck at the 3rd sentence.  It all works, but the why still escapes me.

 

Q4) Given a cylinder labelled with a rectangular grid of positive numbers such that each number has exactly that many neighbours of the same value determine how many such labelling exists, modulo 1 billion and 7.  Two labellings are considered distinct only if they aren’t rotations of each other.

A4) Brute force on the small input here seems scary, but back tracking algorithm stands a good chance of running in time, because the problem is so restrictive.  Some smarts might still be needed.  Pure brute force is 3^36, which is obviously too slow. (4 and higher can’t be on the board, 5+ trivially, 4 because it implies an infinite board.)

Looking more deeply unlocks this problem.  There are limited number of ‘units’ which a solution can be made up of.  A row of 2’s where nothing above or below is a 2.  2 row’s of 3 where nothing above or below is a 3.  These 2 are the obvious ones given in the question itself.  The trick is to come to understand there are only 3 more building blocks.  They all involve just 1’s and 2’s.

122
122

112222
222112

1222
1212
2212

Each can only be placed next to itself in a row, and each can be rotated a number of places equal to its width, but again rotated versions can only be next to rotated versions.

The problem then becomes a DP over rows.  2 rows of 3s separating one of 4 scenarios of 2’s.  One which uses one row, 2 which use 2 rows, but have 3 or 6 rotation variants, one which uses 3 rows with 4 variants.  The trick is to make sure you don’t over count.  Each variant is like a rotation, so if you only use one pair of rows of mixed 1 and 2, you have actually over counted by exactly 3 or 6 times.  One solution is to assume that you are always going to over count by a factor of 12, so divide the result by a factor of 12.  Then at the deepest level of your recursion, force over counting to be 12.  If you’ve not used any 1 or 2 mixed, return 12, if you’ve used only the first kind, return 4, only the second kind (or second and first), return 2, only the 3rd kind return 3, both the 1st and 3rd or 2nd and 3rd (or all 3) return 1.  The DP is then on rows and 4 bools representing whether you’ve ever used each of the 3 types of 1,2 mixed and whether the last set of rows were all 3’s or not.

GCJ15 R1C

Three easier questions this round, although I have to say I found the ‘easy’ question the hardest… Advancing cut-off was either of the first 2 problems and the smalls from both the others and a few minutes to spare.  Solving the 3rd problem and smalls of the other was worth 1 more point and ~400 places as a result.

Q1) In a game of finding all the locations of a 1xW rectangle in an RxC space, where before each guess the opponent is free to move the 1xW rectangle anywhere that is consistent with the previous answers, determine the minimum number of guesses to guarantee victory assuming the opponent plays to maximize your guesses.

A1) So I spent a while rabbit holing entirely the wrong direction.  I seem to have ‘divide and conquer’ on the brain, this is the second time I’ve gotten stuck on a problem this year because I was convinced that dividing the problem in half was optimal.

So in practice this question boils down to forcing the opponent to have no options as fast as possible.  This starts by reducing the places the rectangle can be.  The fastest way to do this is obviously (in hind sight) to guess W cells away from an edge of where it could be.  The opponent will answer no unless it has to, a yes easily reaches a smaller number of guesses.  No answer eliminates W spots.

When you are down to one last row with 2*W-1 or less possible spots the strategy changes.  W spots you know everything, so just guess every spot left to victory.  Between 2*W-1 and W+1 inclusive you know some subset of the middle. Guess immediately next to that, the opponent will answer no (or possibly yes if there is more than one unknown spot left, it makes no difference to them if they are assume we play ideal).   Once they answer no, we know everything and can guess all the remaining spots.

In the end this entire problem can be boiled down to C/W*R + W – (C%W==0 ? 1 : 0).  Each row other than the last takes C/W guesses to eliminate entirely.  The last takes C/W – 1 guesses to get down to the last section, then W+1 guesses to pin everything down.  With one exception if C%W == 0, the last section is W spots, so only W guesses are required to pin everything down.

In the end I wonder why the limited the large input to just 20 x 20, I guess it leaves open a DP solution rather than just the greedy.

Q2) Given a list of letters (size N) to choose from at random, a length of output (size S) and a ‘target’ sequence (size T), determine the maximum number of times the target sequence could appear, then determine the difference between that and the expectation value for number of times it appears in the output if generated at random.

A2) So this is a deceptively easy problem.  I usually don’t like probability problems, but in this case the details clearly point out that multiple overlapping instances all count.  Hence the probabilities are independent and easily calculated.  List of letters can be turned into base probabilities by summing 1/N for each letter.  The target is then converted into a probability by the product of those base probabilities for each letter in the target.  The full expectation value is then that ‘target’ probability times S-T+1, since each possible starting point is fully independent.

The maximum number of times target sequence can appear is determined by the maximal non-complete overlap of the target sequence with itself.  Which can be determined with a simple nested for loop.  The maximum is then (S – T)/(T-overlap) + 1.  One length T, then as many T-overlap sections that will fit.

Q3) Given a list of values that you can have up to c of each of, determine the minimum number of extra values in order to be able to create every possible total between 1 and V.

A3)  I found this question easy, but only because I’ve had previous experience with this kind of question before.  Its a greedy problem.

Start off with having used none of the provided values.  With this you can make 0.  Take the smallest unused provided value, if it is what you can currently make + 1, or smaller, add c * that value to what you can currently make.  Otherwise add c * (what you can currently make + 1) and increment the extra values you had to have counter.  Repeat until you can make V or higher.

One corner case to be careful of for large input. While the target V can never be more than 1 billion, and the provided values are always small, the extra values you have to add can be very closer to 1 billion, so adding c * extra value can overflow a 32 bit integer very easily.

TCO15 R1C

TCO hasn’t really been drawing the crowds this year – another relatively low turn out again for R1C.  Only 815 people registered out of 2500 available slots, 750 to advance (in theory), but again the positive score requirement kicked in and only 622 will advance as a result.  So including byes there is only ~2150 people advancing to round 2.  The problems were pretty easy this time round, although problem 2 wore a pretty good disguise.  I think I could have solved all 3.

Q1) In a given 2xN grid, no two filled in cells are touching by edge or corner.  Without clearing any of the existing filled in cells, what is the maximum number of filled in cells that can be without any 2 touching by edge or corner?

A1) So this problem doesn’t look that difficult, but it becomes trivial once you realize that in a 2xN grid, if no two filled cells can touch by edge or corner, there is no change in restriction if you move every existing filled cell to the first row, and delete the second row entirely.  Obviously only at most one can be filled in any column, and if it is, both of its neighbouring columns have to be empty.

Once the problem is reduced to 1xN, its trivially a greedy left to right fill in while both neighbours aren’t filled in – then count how many are filled in.

Q2) Given a rooted tree and defining a path as being a sequence of one or more distinct vertexes where consecutive elements in the sequence are connected by an edge, determine the maximum number of paths that you can select at once, such that no element of any one path is an ancestor of any element in any other.

A2)  Key here is that a path is a sequence of one or more vertexes.  But every vertex you add to a path can only decrease the number of other paths that could be selected such that there is no ancestor commonality. Therefore the whole paths thing can be disregarded, the question is just how many vertexes can you select such that none are a parent of any others.

Given a non leaf node in a tree, if it is selected, none of its (direct or indirect) children can be selected.  So if it has more than one direct child, it is obviously better to replace it with its children instead.  If it does have only one child replacing it with its child doesn’t make anything worse, but it does potentially lead to a further replacement if there a branching factor greater than 1 anywhere in its indirect children.  Ultimately this means there is no point selecting anything other than leaf nodes.  Leaf nodes also cannot possibly be an ancestor of any other leaf node or vice versa, so the problem reduces down to counting the leaf nodes in the tree.

Q3) In a list of boolean values, its ‘value’ is equal to the number of distinct subsections which are entirely made of alternating 1’s and 0’s.  A subsection of length 1 is considered to trivially ‘alternating’.  For a given n and k determine how many possible lists of boolean values of length n have ‘value’ k.

A3) This problem is clearly the hardest of the 3, but it has a fairly obvious dynamic program style solution.  A sequence of length i ending with alternating section of length j, with existing value v, can either be extended to i+1 with alternating length 1 with value v+1, or length i+1, alternating length j+1 with value v + (j+1)*j/2.  So if you have x of such sequences, you can add x to both of the destination cells.  Start with a seed of length 1 with alternating section length 1 and value 1.  Each target cell is always strictly greater in i and v, so iterate over j last.

Note that I didn’t talk about 0 or 1 here.  But you don’t need to, every sequence has a complement of same value by swapping all 0 to 1 and vice versa.  So the basic program output just needs to be doubled.  Then finally the n and k from the original input map to i and v, and you sum over all j.

GCJ15 R1B

This was a round of relatively tough problems.  In the end solving the first and the small of the second was sufficient to advance, so long as your time penalty wasn’t excessive.

Q1) Given the increment and reverse digit order operations, what is the minimum number of steps to get from 1 to x?

A1) So the small input can be brute forced using a breadth first search, but why this is true is one of the key points towards the large input which cannot be brute-forced.

The danger with any breadth first search is that the search space might explode.  But in this case a quick inspection finds that a search to find x never considers values greater than 10^(ceil(log10(x))).  Of the operations available, only the increment can increase the number of digits.  Therefore in order to get to a value in the next tier, you have to first get to 9999…  It is therefore interesting to see how many steps it takes to get to each tier.  Inspecting the bfs it takes 1 to get to 1, 9 more to get to 10, 19 more to get to 100, 109 to get to 1000, 199 more to get to 10000.  This pattern leads to the insight of what the minimum number of steps involved is to get from one tier to the next.  First increment until the last half of the digits are all 9, then reverse, then increment until next tier is reached.

Having made this observation it is natural to jump to a greedy approach, calculate number of steps for each tier, then on the last tier increment until the last half of the digits match the start of the desired number, reverse and then increment until the desired number is reached.  There is one corner case however.  If the target number ends in zeros, reversing leaves you with a number ending in 1, and you can’t go backwards.  The natural fix here is to aim at the first half of the digits – 1, reversed, as the last half of the digits.  So for 9900 the first step is to get to 1089.

This just leaves proof the approach is right.  The greedy algorithm is pretty obviously the minimum number of steps which stays within a tier, but we need to rule out going up a tier, reversing a number ending in zero.  This falls out from the number of steps the greedy algorithm takes to get to a number worst case within a tier.  For anything that doesn’t end in half zeros, the number of steps is obviously less than the number of steps to get to the next tier, since each sub part of the algorithm has less steps.  This leaves 90, 900 and 990 as a potential worst case scenarios.  First takes 18 steps (less than 19) from 10.  The second takes 108 steps (compared to 109) from 100.  Finally the last takes 99 steps (compared to 109) from 100.  So going up and then down is never going to be cheaper.

Q2) Given RxC grid and N filled in squares, what is the minimum number of shared edges.

A2) The small input is only 15, so brute force simulation is nice and easy.   Conveniently 15 is also 3×5, which ensures good coverage of the main corner case.

The large input seems a natural ‘greedy’ approach scenario.  Checker-board coverage, then fill in any empty corner locations (for 2 edges each) then any empty edge locations (for 3 edges each), then the centre (for 4 edges each).  But this fails.  Inspecting brute-force vs greedy the differing scenario is in 3×5.

Greedy for N=12 gives:

xxxxx
xx.xx
x.x.x

Brute force on the other hand gives:

xxxxx
x.x.x
xx.xx

Greedy scores 12, brute force scores 11.

At this point I have to say I was stumped, I started to consider DP style approaches, but they were all very very computationally expensive, the large input was not plausible.  So I looked at one of the submitted solutions.  And now staring at the cases I presented above it seems obvious, but maybe it would have been more obvious if I had of presented N=11.

Greedy vs Brute-force for N=11.

xxxxx xx.xx
xx.x. x.x.x
x.x.x xx.xx

For an odd by odd grid, there are 2 ways to tile checker-board style and the maximal one is not always the best option as a starting point for the greedy algorithm.  A single extra spot has a cost of 3 in maximal grid, but in the inverted grid the first 4 extra spots cost 2.  The inverted grid fills one less spot, so at 2 spots at 2 each vs cost of 3 is a loss, but 3 spots at 2 each vs 2 cost of 3 is a draw, and 4 spots of 2 each vs 3 of 3 is a win for the inverted grid.  Ultimately the inverted grid starts losing again because it has more interior points which cost 4, but while its edges are still being filled in, it gains and then holds the advantage.

Ultimately 3×5 is not required to force the inverted grid, 3×3 has a win for the inverted grid at N=8.  But 3×5 does cover the full progression of cases better.

Q3) Given a bunch of people walking endlessly at constant speeds around a circular track, with varying starting points, determine the minimum number of times to meet up with anyone else in order for you to do one loop of the track, if you can do any speed you want whenever you want.

A3) So I think that your super powers with regards to speed means the problem can be turned into one of arrival time.  If you arrive at the destination at time x, you have to have gone past anyone who hasn’t yet completed a lap (for 1 meeting each) and have one additional meeting for every time you have been ‘lapped’ by someone who has completed 2 or more laps.  Trivially infinite speed with an arrival time of 0 gives you an upper bound which is equal to the number of walkers.  It also seems obvious that an arrival time larger than that of the slowest walker’s first arrival is not worth considering, it is a possible minimum, but any longer you can only be lapped by more walkers, so it can’t be any lower. The trick is to determine the potential arrival times of interest and calculate the number of meetings in time.

It is easy enough to sort everyone by their first arrival time, this gives a bunch of ranges to calculate the number of times you get lapped, but the naive approach of calculating number of times you get lapped is O(N) for each arrival time.  And O(N^2) is not going to cut it for N=500000.

In between each arrival time the number of meetings can only go up, when you get lapped, the instant after an arrival time is the only time it can go down, and it only goes down by one.  This leads to an observation.  Arrival time 0 gives an upper bound on the answer of 0 and if you are considering arrival time of the kth earliest first arrival and the number of meetings which have occurred is greater than N+(N-k), there is no way the remaining meetings can get the total meetings below N.  Therefore there is no value in continuing once you have reached 2N-k meetings, or more simply 2N.  2N is 1 million.  So if we can simulate ‘every’ arrival time (not just the first arrivals) in O(log N) time each, and early out once we hit 2N, we can run in time.  Simply putting walkers in a heap ordered by their next arrival time, and putting them back in with their next arrival time, will give the desired behaviour.  Just need to track whether its first arrival or not in order to decrement or increment the times.

Oh and there is likely a corner case to do with equal arrival times, they have to be processed as a group because you can’t arrive in between them, only before or after.  And getting accurate equal arrival times requires using fractions rather than floating point.

.Net 4.6 RC Diff

So its been a while since I last did a framework surface diff, but it seems my program still works.  This time I diffed 4.6 RC vs 4.5.1.

A few small things, but hiding in the middle is a bunch of new classes in System.Numerics which is nice.

As usual this list is not complete, I skip things which I don’t think are worth mentioning.  But that isn’t many things this time.

  • System.Array.Empty<T>() – does what it says on the box.
  • System.Buffer.MemoryCopy – like BlockCopy, but for raw memory pointers rather than arrays.
  • Usual set of SQL Server database connection additions – column encryption, authentication types.
  • DateTimeOffset supports conversion to/from unix time in seconds/milliseconds.
  • System.Diagnostics.ProcessStartInfo now has an Environment property.  Not sure what it does yet, given there is already the EnvironmentVariables property.
  • Bunch of Event tracing stuff I have no idea about…
  • System.FormattableString – seems to bunch a format string and its arguments together – but has no public constructor… But there is a static create method on System.Runtime.CompilerServices.FormattableStringFactory.
  • System.GC – new ‘no GC’ region methods which ensure there is enough memory before they start.  Can also force a small object heap compaction.
  • System.Globalization.CompareInfo.GetHashCode – can get a culture specific hashcode.
  • System.IO.MemoryStream.TryGetBuffer – can get back an array segment when memory stream is constructed with offset/length.
  • Async methods for NamedPipeStream connection.
  • Async methods for read/write/flush on UnmanagedMemoryStream
  • Socket options for reusing ports.
  • System Numerics adds Matrix3x2, Matrix4x4, Plane, Quaternion, Vector2, Vector3, Vector4.  They are unfortunately all single precision, but the reasoning is justified – they are all hardware accelerated using SIMD.  Interestingly the pre-release 4.6 documentation appears newer than the actual released code here.  It documents many useful static methods which are not yet in the actual library.  Also there is apparently a System.Vector<T> coming which works for any primitive type T, with a maximum rank dependent on SIMD register width.
  • New assembly level attribute – DisablePrivateReflection – apparently does what it says…
  • Asymmetric padding mode options for crypto.  CNG supports RSA.  Crypto random number generator can write random values to a subset of an array.
  • WindowsIdentity.RunImpersonated – can now execute a delegate rather than having to set up an impersonation context.
  • string.Format with format provider now has the small arg count optimization of explicit methods for 1 2 and 3 format args.
  • System.Encoding has dropped default support for code page based encodings, you have to manually call RegisterProvider to have them supported??
  • System.Encoding has a GetString which takes a byte pointer and length rather than just an array.
  • StringBuilder has an Append which takes a char pointer and length rather than just an array.  Also small arg count optimization with AppendFormat with format provider.
  • System.Threading.AsyncLocal – interesting, not 100% clear what it does yet, but it sounds like thread local variables except with a value per ‘async flow’ rather than per thread.
  • Task creation methods for immediate cancelled/exception states.
  • Extension methods to get a safe wait handle given a wait handle.
  • System.Uri.IdnHost – can get punycode version of international domain names.
  • WPF diagnostics hook event for visible tree changes.
  • System.Xml.XmlNode gets a PreviousText property.

TCO15 R1B

Having passed through already I didn’t compete in this round, but I was interested to see how the turn out was without a Google Code Jam happening at the same time.  Only 1023 people registered, which after allowing for the 700 advancers last time is only ~400 new people (assuming everyone who competed last time and didn’t advance, competed again) that became available without the code jam running.  Looks like round 2 will probably be quite short of its 2500 target if this keeps up.

Positive score criterion appears to have been the cut-off again, only 591 advancers.

Q1) Find the size of the largest sub range of an array where at least half of the numbers have a common divisor other than 1.

A1) With the array size limited to 50, and the size of the values limited to 1000, this seems a trivial brute force.  There are 1250 sub arrays of average length 25 and (obviously) less than 1000 prime numbers to test for divisibility.  There apparently 168 prime numbers less than 1000, so its not a huge win, but it does help a little if you happen to be working with a particularly slow programming language I guess.

Q2) Determine the expectation count of objects found if each object has a probability of being found on its own, and if found a list of other objects that will definitely be found (which expands transitively).

A2) Up to 50 objects, so a brute force of every combination of basic find on its own chance is out of the question.

Each object can be expanded out transitively easily, but the question is how to combine the probabilities is not obvious.  If an object is in isolation, nothing connects to it, the probability is simple.  If the object is part of a cycle which has no external connections, the probability is 1 – product(probability of each member of cycle not being selected).  Its the external connections which are trickier.

I think all cycles can be collapsed to be replaced with a single node with a ‘base’ probability as described above.  Once the cycles are removed, the graph is now a directed acyclic forest.  The roots are obvious they have their base probability and that’s it.  Children then have probability of being chosen of 1 – product(base probability of not being chosen, and total probability of not being chosen for each parent) – remembering that each node can have multiple parents and we have to have calculated the total probability for each parent before we can start.

Apparently I’ve made the problem more complicated than required though – in effect the probability of an item being found is 1 – product(base probability of not being chosen) for every node that is transitively connected to the item in question.  I can see how the math works out to be the same, but it wasn’t an obvious starting point.

Q3) Given a tree determine the number of distinct subsets of 7 vertices which are connected to such that the second is has the first as a parent/grandparent, the 3rd 4th and 5th has the second, and 6th and 7th has the 5th.  Subsets are distinct only if they have different members, order doesn’t matter.

A3) Number of vertices in the tree is up to 2000, so brute force is trivially out of the question.  It would seem a multiple pass dynamic program is in question.

First pass is number of transitive children for each vertex.  Second pass is number of ways to select 2 children of a given vertex.  Third pass is sum of products of the second pass for each child and the number of ways to choose 2 other children which are not the selected child or its descendants.  Fourth pass is sum of the 3rd pass for each child.  Final answer is the total of all values in the 4th pass.

The 3rd and 4th pass enumerate all transitive children for each vertex, this will only perform acceptably if you store the children as an adjacency list rather than checking for every potential vertex as a child in an adjacency matrix.  Additionally the number of ways to select 2 children needs to be done as N*(N-1)/2 – not by enumerating.  Calculating N in the second pass is trivial from first pass, N in the 3rd pass is first pass value minus the first pass value for the currently selected child minus 1 more for the currently selected child itself.

TMD.Algo now on GitHub (also 0.0.6.0)

I’ve migrated TMD.Algo from my internal source control to a GitHub public repository.  It can be found here.

As part of the migration I have dropped the signing key, so a simple download of the project will actually compile, but adding the built result to the GAC (if you so wish) will involve a bit more work.

Also along with the move to GitHub comes some work I’ve done on the library over the last couple of years, so I’ve upped the version number to 0.0.6.0.  The major new feature is the GCJ class under TMD.Algo.Competitions.  This class is designed to be the main entry point of a program parsing Google Code Jam style input and producing Google Code Jam style output.  It simplifies the basic parsing logic.  It also optionally supports running test cases in parallel, for those times your code just doesn’t quite optimize fast enough to otherwise solve in time.

The majority of the work for 0.0.6.0 however was in starting a new set of integration tests.  These integration tests take custom written solutions to past GCJ problems, which attempt to use the TMD.Algo library as much as vaguely makes sense, and ensure that they can handle the practice sample/small/large inputs to produce outputs that GCJ practice website considers passing.

Other smaller changes can be found in the commit description.

GCJ15 R1A

Over 12000 qualifiers could have turned up for R1A, but given time zones that was always unlikely.  5032 positive scores, over 300 perfect scores, and the advancing cut off was a fast time on the first 2 problems completely solved.  If I had of been doing this round I think I would have advanced, but maybe not having completed the easiest question – as I think the wording is incomplete, leaving the question open to being interpreted as a much harder question.

Q1) Given a list of observations of the number of items on a plate at 10 second intervals and knowledge that there is someone who can add items at any time, and another person who removes them either whenever, or at a constant rate if the plate is not empty (but not both).  What is the minimum number of items the second person removes under each mode of item removal.  Presume that constant rate item removal is a continuum, if removing 1 per second, there is half of the item still on the plate at 0.5 seconds.

A1) So I’ve changed the wording for this question to be what was actually marked correct, there is no statement regarding whether items are removed from the plate discretely or gradually across the period of constant rate of removal.  Removing discretely is a much different question, which is quite a bit harder.  (And given the persons ability to remove arbitrary numbers of items at any point in time in the first mode, my mind gravitated towards discrete.)

To see where this is different consider the example 1 1 1 0.  Under the continuum removal case the number of items removed per 10 second period must be an integer, as all observations are integer.  Hence 1 item per 10 seconds is the minimum removal rate, and 3 items are removed.  But if removing is a discrete event, you can choose a removal rate of 1 every 25 seconds, in which case only 1 item is removed. (Assuming that the discrete removals happen at the end of each removal period rather than the start – the start you have to remove one immediately, so the total is 2.)

Given the continuum restriction this problem is trivial.  In the first mode of removal you remove items only if the count goes down from observation to observation, so just sum the neighbour differences where they go down.  In the second mode the minimum number removed is described by the minimum rate – and the minimum rate is given by the largest drop.  Once largest drop is found the answer becomes simply a sum of all the values (excluding the last) except if the value is above the largest drop, then you just add the size of the largest drop instead.

The non-continuum problem is much harder as you have a search space which is not limited to integer values per 10 seconds, and its not clear that it is even a single transition from too slow to fast enough which would allow it to be binary searched.

Q2) A list of barbers each have a well defined customer processing rate.  When will the nth customer be served?  Assume that if multiple barbers are free, they are filled earliest in the list first.

A2) Nice problem.  The size of N may be huge meaning simulation is out of the question, even for the small input.  For the small input it might be possible to find a period of repetition of patterns of when barbers become available and skip ahead as appropriate, but the large input clearly rules that approach out as well.

While a simulation is not feasible, it is possible given a time T to determine quickly how many people each barber has started serving by the end of that minute, its just (T / barbers_time_to_cut) + 1.  The same formula given T – 1 gives how many people had started being served before this time.  This gives a range, which if it contains N, you’ve found the minute N starts being served and all that remains is to determine which barbers changed state between T -1 and T (which comes from the same formula), and choose between them based on how far N is from the start of the range.  To find which T-1 to T contains N, binary search on T.  If N is before the value at T – 1 T needs to be lower, if its greater than the value at T it T needs to be higher, otherwise you’ve found the value.

Q3) Determine, for each vertex, how many other vertexes need to be removed, for the specific vertex to be on the convex hull.  Consider co-linear points to form a convex hull which touches all of them.  All input vertexes will be distinct integer coordinates.

A3) The small input only has 15, so a brute force trial of every subset using the standard O(N log N) convex hull algorithm (like in TMD.Algo) – would work.  However the one in TMD.Algo throws exceptions if you give it a subset smaller than 3 or perfectly co-linear points, so those cases have to be handled separately.  For each node on the convex hull from each subset, it gets a new potential minimum based on the size of the subset vs the original input size.

The large input is much harder – 3000 obviously can’t be done with an exponential cost algorithm.  However with a fast computer and lots and lots of cores… an O(N^3) approach might work, if it has a sufficiently low constant.  Consider each pair of nodes gives N(N+1)/2 options.  These 2 nodes define a line of the form ay + bx + c = 0 (or a dx,dy vector).  Now every other point can be substituted into the formula (or cross product with dx2,dy2 vector formed by subtracting the point from original point  used to create the dx,dy vector).  Count the number of positive, negative and 0 outcomes (from either case).  Since we are considering every possible point pair, then obviously one of those pairs will be an edge on the ideal convex hull, for one of those points.  For the edge to be part of the convex hull, either all the positive, or all the negative points must be removed.  Thus the size of the smaller of the two options is a potential minima for both of the original two points.  Calculating the line formula is 2 products and 2 additions, then 2 conditional increments to classify.  Giving a constant for N^3 of 1 multiply, 1 add and 1 conditional increment, each of which is potentially only a clock cycle on a modern cpu.  The cross product approach is 3 subtractions instead of 2 additions, but should also be reasonable.

But if your programming language doesn’t optimize well, or you don’t have a bunch of cores available there is an O(N^2 log N) approach, which can be considered slightly inspired by the standard fast convex hull algorithm.

For each point sort all other points by angle relative to that point.  This can be done by first dividing the points into left/right and using a standard sort algorithm on each side using cross product of each point’s vector relative to the starting point as the comparator function.  Once the points are sorted O(N log N) the minimum removal of the original point can be calculated in O(N) time.  For each sorted point, it defines an edge with the starting point, and the rest of the sorted points are either left/right or equal of that edge.  The counts of these left/right/equal can be efficiently calculated in general, by first doing a linear scan of the sorted list for the first candidate edge, then to do the rest of the candidate edges you just have to advance the the indexes in the sorted list that mark the interesting areas, left, right, equal but on same side of starting point as candidate point, equal but on opposite side of starting point as candidate point.  In the process of going through all candidate edges each marked index will never need to backtrack more than one index per step, so they all have a maximum of O(N) operations each to maintain, giving a total of O(N) operations. (An alternative to all this state maintenance is to for each candidate edge binary search to find each of the 4 transition points.  Its O(log N) rather than O(1) per candidate edge, but given the original sort is O(N log N), this is not significant.)

The above algorithm is simply repeated for every input point, giving a total of O(N^2 log N)

GCJ15 QR

20 points to advance meant that a full solution to the simplest problem wasn’t sufficient.  Even so 12438 people advance to round 1.  23296 people got a positive score.  Makes the 1371 people who turned up to topcoder open pretty tiny, although maybe the simultaneous scheduling of code jam qualifying round and topcoder open was part of why topcoder open’s numbers were so much lower than usual.

348 perfect scores, despite the problem worth the most points having significant scenarios in the large input that were not even close to covered in the small input.

Q1) Given a count of the number of people who will stand up for each threshold of people already standing up, determine the minimum number of extra people (of any mixture of thresholds) required to make everyone stand up.

A1) This is a basic greedy problem, consider each threshold with non-zero population in order, if there aren’t enough people assume there are by adding the difference between how many are standing up and need to be standing up to the already standing up set.  Accumulate these differences and return.  This works because larger thresholds won’t stand up before earlier thresholds, and so you have to add enough to trigger the earlier threshold.  Large input is only 1000, and the solution is linear anyway, so no problems with running time.

Q2) Given stacks that go down by one every minute, except if you pause everything and move a subset of one stack to anywhere else, including making a new stack, what is the minimum time for all stacks to become empty?

A2) So my first instincts here were wrong, a single stack of size 9 can be done in 5 minutes, but my first instinct was to only ever divide stacks into two, which gives 6 minutes as best effort here.  The correct approach is to take 6 (or 3) off , then 3 off the remaining stack of 6, then allow 3 minutes to run its course of the 3 stacks of size 3.  Interestingly the small input includes stacks of size 9, so at least this mistake wouldn’t slip past to the large input.  The high percentage failure rate on the small input here suggest I may not have been alone in missing this key point at first.

To get the large input done in time it is sufficient to solve the problem in O(NK) time, so one option is to consider best time where you split tall stacks down to a maximum height of m before allowing time to run.  Height ranges to consider are from 1 to tallest stack, and number of turns to split a stack down to at most height m is given by simple division, so the total of all stacks can be done in O(N) time.  Giving a total of O(NK) N being number of stacks and K being the height of the largest stack.

Assumptions at play here are that it is always better to split first before letting time run.  This is pretty obvious as if a split is worth doing, doing it sooner means more pancakes per second are removed later.

I think there is an approach with better runtime characteristics, which involves considering only a subset of all heights, something like O(N*Sqrt(K)*Log(N*Sqrt(K))).  For each stack generate the the set of heights for splits down to sqrt of its height, sort all of these in decreasing order and consider the ones greater or equal to sqrt of the largest height.  Each step through this list of heights corresponds to one extra minute doing a split, and defines a sequence of heights of highest remaining after n splits.  Then just linear search for best.  This approach presumes it is never any better to divide a pile beyond its square root, which seems straight forwardly true, dividing things further takes more than one extra split per height reduced.

Q3) Given a potentially repeating sequence if i’s j’s and k’s, determine if it is possible to subdivide them into sections which evaluate to i, j and k respectively assuming that the i, j and k’s represent standard quaternion’s and are being multiplied using standard quaternion multiplication.

A3) The problem helpfully explains that quaternion’s satisfy A*(B*C) = (A*B)*C.

One approach (which is a bit slow, but just sufficient for the large input) is to determine all prefixes which can create i, all the postfixes which can create k, and determine if the gaps in between create j.  The fact that the sequence can be repeating (and the repeat count can be huge) appears to be a problem at first, but a close inspection of quaternion powers reveals this is not going to be a problem.  A fairly quick inspection finds that any quaternion to the power 4 is 1.  Hence repeat counts mod 4 are the only cases needing to be considered.  So, calculate the value of the entire repeating sequence, and consider each of its 4 powers as a pre multiple of any prefix of the repeating sequence.  If the answer is i, store the pairing of the mod 4 power and the prefix length.  Similar for k, but post multiply and postfix of the sequence.  Both of these sets can be generated on O(N) the size of the repeating sequence which is at most 10k.  Each set is also O(N) in size, so we have to be able to determine whether there exists a j in between in O(1) time, as anything over O(N^2) is clearly too slow.

Given a candidate i and k prefix/postfix – there are 2 possibilities, the prefix postfix meet at the same central location and the j is the middle section of that repeated segment, or they have a larger gap in between, and j is a postfix (optional repeats) prefix sequence.  The later is easiest, the postfix length is defined by the prefix length of the candidate i, the prefix length defined by the postfix length of the k candidate and the number of repeats, mod 4 is defined by the mod 4 of each of the i and k candidates and the total number of repeats.  Care must be taken if the number of repeats is small that the only mod 4 value which satisfies the criterion, might be negative.  If you have a cache of prefix/postfix values and powers of the repeated section, this becomes 3 quaternion multiplies.

The first case where the 2 touch is more difficult.  Need to check the mod sum vs the total repeats works out, since the j section doesn’t have any repeats this time.  But unless you pre-cache the product of every subsection of the repeated section, it can’t be done in O(1) time.  That pre-caching is another O(N^2) cost, and a lot of memory, so nice if we can avoid it.  The solution is quaternion division (pre/post) is well defined.  Each quaternion multiplication is a permutation, so for a result and one of the input multipliers, there is only one possible value.  We know the value for the product of an entire section, and the value of the prefix and postfix, so we can do prefix division and then postfix division to determine the value of the middle part.   This is O(1) time, as we need.

However, there is a better way! – if the sequence can be broken in to i, j and k subsections, its total product must be the same as the product of i j and k, which is -1.  So if the total product if the entire sequence (including repeats) is not -1, we can exit out early.  If it is, there is no need to verify the value of j, instead all that is required is to verify that there exist i and k prefix/postfix, and those do not overlap.  So generate the i and k prefix/postfixes as before, but select the shortest of each.  Now there is no longer an O(N^2) inner loop, just a check of the 2 shortest prefix/postfixes do not overlap.  If they don’t, then the inner section is known to be j – given by the fact that quaternion division is well defined and knowing the total product.

Q4) In a game where an opponent gets to choose one n-omino, and you have to place it in an RxC sized container and then fill the remaining space with n-ominos of the same size (but not any specific shapes), determine whether for a given RxC and n the game is always winnable by the opponent (as in they can always choose an n-omino that you cannot place and fill the surrounds).

A4) The small input is interesting here because the number of test cases is 64 exactly.  This corresponds to the number of possible small inputs! – R, C and n are all limited to the range 1-4.  The large they are limited to 20, meaning there are only 8000 possible inputs.  You could theoretically write a less efficient program, brute force them all and hard code the result.

There is one main early out.  If R*C % n is non-zero, it doesn’t matter what the opponent does, they always win by default.

Another easy scope reduction is if R is larger than C, switch so R is the small one and C is the larger.  Then if n is larger than 2R it can be made into an L shape that cannot possibly fit, so the opponent wins.  This is actually almost sufficient for the small input, the one remaining case is the t piece can be selected in 2×4 for n=4, which splits the the space into a 1 and 3 areas which can’t be filled.

The large input covers a lot more scenarios.  But they can be brute forced by hand, if you are careful.  The 17% pass rate suggests cases are easy to miss… First there is a hint in the problem itself, it shows some of the 7-ominos, one of them has a hole.  Obviously the opponent can always choose this piece and make tiling impossible.  This is trivially extended to all larger n-omino’s.

Before I cover the remaining scenarios it is worth mentioning that if the opponent can’t win for AxB, anything larger than AxB that satisfies the mod n condition also can’t be won by the opponent.  So we just have to start small and work our way out, as soon as we get a case where we win, everything larger is a given.  It is fairly trivial to see that some simple zig-zag filling can be used to fill any L shape or rectangle shape that you might add to an AxB to make it a larger case, so long as these L/rectangle shapes themselves are a multiple of n in area.

So lets cover things in order

  • n = 1 – trivially no opponent win.
  • n = 2 – trivially no opponent win if mod satisfied
  • n = 3 – no opponent win if R > 1 and mod satisfied, otherwise opponent win – again pretty trivial.
  • n = 4 – opponent win for R = 1 by L shape.  As mentioned before the t shape is opponent win for R = 2 and C = 4, but this extends to R = 2 in general, as it leaves both sides of wherever you place it with a 1 mod 2 value, which can’t be tiled using pieces of size 4.  R = 3 and higher always works because 3×4 can easily be tiled regardless of opponent choice. (There aren’t many choices for tetromino’s so they are easily worked through.)
  • n = 5 – here is where it gets tricky… base is opponent win for R <= 2 using the L shape. R >= 4 is no opponent win, not trivial to work out, but you can consider all 12 by hand for 4×5, and then larger falls out.
    R = 3 is the gotcha case.  The tricky piece is the diagonal step shape – also known as ‘W’ under standard pentomino naming.  Regardless of rotation in an R=3 space this divides the space into two parts.  For C = 5, the size of these parts is 3 and 7, or 6 and 4 depending on placement.  Neither of these can be tilled.  But for C = 10 or higher, it can be placed so the spaces are 15 and 10 (in the C = 10 case, similar in larger), which can be tiled easily.
  • n = 6 – again L shape gives opponent win for R = 2.  R = 3 is an opponent win by the dagger shape a 4×3 cross which divides the shape into 2 mod 3 and 1 mod 3 spaces, which can’t be tiled by size 6 shapes.  It also can’t be tiled due to the 3×3 ‘space invader’, despite being able to place it in any rotation, which I think is cool…
    R = 4 – this time there are 35 different 6-omino’s to brute force, but none of them cause trouble in 4×6, and hence all larger is no opponent win if mod condition is satisfied.
  • n >= 7 – the piece with a hole gives guaranteed win to opponent.

I missed the W shape in n = 5 when I tried to work these things out by hand, I wonder what the most common mistake was for the large input.

I also wonder how many people tried to actually solve the puzzle programmatically rather than hard coding rules from by hand brute force.  This is not trivial as it involves determining whether you can tile a connected space or not and it is possible to create a ‘t’ junction which means even if the space to fill is a multiple of the n under question, it depends on how many pieces are on each side of the ‘t’ junction as to whether it can be connected.  Its also tedious generating all the n-ominos (if you don’t hard code them) and writing reflection/rotation generation and sliding them all around.  And for the largest sizes you might risk time-out, unless you take advantage of the expansion proof where smaller cases answer larger cases I mentioned, and pre-calculate the answer for all possible inputs (from small to large) before running the 100 test cases.