GCJ10 R2 Analysis

So, 500th place was the same score as me, just 30minutes faster.  If I had of started with question 3 rather than wasting almost an hour on question 1, I would have gotten through.  Had I submitted anything else successfully I would have gotten through.

Exactly 2 Australian’s got through, in 498th and 500th respectively.  I was the third highest Australian, and given how I was doing in the first round, 615th was a fairly successful result, just disappointed I don’t get a t-shirt.

On to the questions…

Q1) Give a set of numbers laid out in a diamond shape (for instance 1 number on the first row, 2 on the second, 1 on the third), determine the smallest diamond shape of numbers which contains the given diamond, and is horizontally and vertically symmetric.

A1) This question had a bit of controversy, up until about the 50minute mark (which was when I switched to Q3) the question text was actually incorrect.  I would claim this as why I failed to solve the problem, but almost certainly the real reason is because of the midnight start, my approaching head cold and general momentary stupidity :P.

Possibly the biggest pain with this question is the diamond shape.  It is not a nice layout of numbers to work with.  I spent a few minutes trying to get my brain cells to work to write code for detecting mirror symmetry in a subset of the diamond before I gave up and rotated the diamond 45degrees, making it a square.  Even writing the code to do that wasted about 15minutes, which indicates how well my brain was working.  Working with a square with diagonal symmetry checking was much easier, although I still had a bunch of off by one errors I had to fix.  In the end I did get that working, but while my answer solved the sample, it did not solve the small…

As I realised after the competition it was because I was completely screwing up my solution.  I was determining 2 mirror symmetries in a subset and mysteriously assuming that could be used as the seed of the smallest double symmetric diamond which contains the original.  I was at least having the requirement that the subset included one of the corners, but this approach is just wrong.  In the end I have all the code I need to solve the problem, just the wrong algorithm!

I believe the correct solution is to find the largest single mirror symmetric (along the axis which doesn’t pass through the corner) subset for each corner.  Then find the largest sum of pair of non-opposite corners.  These 2 will be the seeds of the large diamond.  Final answer is 3*original diamond size – sum of sizes of pair of non-opposite corners half symmetric subsets. (Give or take some off-by ones which I may have missed…)  All in all given how few points this question was worth, I really should have listened to my first instinct and just ignored it completely.

Final results for the competition are not announced yet, leaving a tiny hope that I might get through due to the controversy with this problem, but it would be a cheap win which I don’t deserve.

Q2) Given 2^p teams competing in a knock-out competition, with each match  in the knock-out tree having a specific cost and a list of the maximum number of games for each team you are willing to miss, determine the minimum cost to attend the required number of games worst case. (p is up to 10)

A2) Having up to 1024 teams scared me away from this question quick smart, but it was the one that most people actually solved.  The small data set every cost was 1 dollar, the large, the costs varied between 0 and 100000.  Even so overflow was not going to be an issue.  Even now I can’t immediately see how to solve this, but I am beginning to think that it involves a divide and conquer approach…  And pretty much as soon as I’ve thought about the question properly for a minute I have solved it.  Define the recursion to be over the minimum cost to satisfy all leaf restrictions of a given branch, having already bought n matches at some parents.  Then for each node in the tree, the result is equal to the minimum of the cost of this node and the sums of each child for having bought n+1 matches, and the sum of each child for having bought n matches.   Terminating condition of infinite cost if we fail to satisfy the maximum games missed for a given team.  Finally the result is the grand final match node with having bought 0 already.  This can be done using dynamic programming, with an array p by (2^p)-1 using the /2 to find the parent, *2/*2+1 to find the children packed tree in array representation.  Or you could dynamic program over an actual binary tree, with each node containing an array… performing a depth first walk.  Or you could do memotized recursion.  Whatever, its all easy – and I really should have taken a closer look at this one!

Q3) Given an ‘infinite’ grid with cells which are filled in or not, where each cell which has a north or west neighbour stays alive and which has both a north  and west neighbour, but is empty, comes alive (simultaneously – so a north east pointing diagonal line of cells slow moves and shrinks to the south east).  And up to 1000 large possibly overlapping rectangles which are initially filled in, determine how long before there are no cells filled in.

A3) I managed to solve this as almost O(N^2) in the number of rectangles.  I suspect with the appropriate data structure checking every pair for ‘overlap’ could be reduced to O(N log N) but O(N^2) is plenty for this questions large constraints. (The almost in the first sentence is the O(alpha(n)) amortised cost for each disjoint set tracker union operation.)

First we divide the problem in to sub-problems.  If two sets rectangles are well separated, they do not influence each other in any way.  So first of all what counts as separated?  If 2 rectangles overlap, then presto that is a guaranteed interaction.  If they don’t come close to touching at all (separated by at least one in any axis), they are separate.  If they are horizontally or vertically immediately adjacent, they act together.  This leaves diagonally adjacent.  Corners touching in the north east/south west directions count, but the north west/south east directions do not.  I used a disjoint set tracker (I love this thing!) to track which act together and which do not.  This part of the problem actually turns out to be the most expensive, since the rest runs in O(N).

Once they are separated we can treat each section individually and take the maximum of each problem.  So given n rectangles which interact, how long do they last?  It is fairly easily apparent that no matter how the north west corners get trimmed at each time step, the south east growth areas grow first.  So the last cell to go will be the south east most part that either grows or is already there.  Growth areas are limited by the south most edge and east most edge of all constituent rectangles.  The tricky bit is determining which of the first cells to go will be the start of the cascade which reaches that final location.  Although actually when it is phrased like that, it becomes somewhat obvious.  Since the area which dies moves south east, the most north west point is going to be the start of the final cascade, all other locations will get stuck against one of the walls belonging to that location.  That may sound convincing but proving it is probably more complicated.  I managed to convince myself well and truly before I wrote my solution, but my reasoning is not a proper proof, although I am sure one exists.  So comparing the north west corners to select the one which lies on the most north-west diagonal, and taking the maximum of the south east corners, we have the ‘effective rectangle’ for the group of rectangles which interact.  A single rectangle takes height + width – 1 turns to disappear, and this also works with the effective rectangle.  So we finally return the maximum of each disjoint set, and we’re done.  Only 60 people got this out, so I was quite happy.

Q4) Given N points which each have a goat tied to it, and M possible locations for a bucket which each goat must be able to reach, determine for each bucket the minimum area which all goats can reach, by adjusting the goat ropes to whatever length you like.

A4) I spent my last 25minutes doing the small problem set for this.  If I had of successfully submitted it, I was through, and I realised that at the time, so the pressure was on.  The small problem set limited N to 2, which is the easiest case of the overlapping circles problem.  When I saw this I thought, cool that will be simple I’ll just jump on mathworld copy the formula into my code and submit it… Which I did.  And it failed.  Oh I have a bug.  Fix that, still fails.  Oh what about this special case, fix that (later realise that the problem explicitly excluded it from being possible), still fails.  Hmm maybe numeric accuracy in the acos function parameters, avoid unneeded square roots… still fails.

What can I say it was 2am, I couldn’t quite activate enough brain cells to realise that the formula from mathworld was wrong…  Well not exactly wrong, more, insufficiently correct.  The mathworld solution only handled the case where the circle intersection points form a line which crosses the segment joining the circle centres.  If the resultant area is made up of 2 circle subsections where one of the circle subsections is more than half of a circle, it produces the wrong result.  In that case the area is the sector + the triangle, rather than the sector – the triangle.  If I had of actually sat down and derived the formula I might have gotten this correct, but 25minutes at 2am, it wasn’t going to happen.

The large problem set, N is up to 5000, M is up to 1000.  Even if I had the formula for the area of a truncated circle and an arbitrary convex polygon already written (which is something I should really get around to doing for TMD.Algo), I suspect I would take a long time to write the solution, and even then I think it would be O(N^2 * M) at best, which is very risky timewise.  I think something like the convex hull algorithm must come in to play.  Only 2 people got this problem out and they came 7th and 27th.   The top 6 got everything out except for this.

I actually think I have seen this question asked in topcoder before, so its probably worth being added to TMD.Algo in its entirety.

So that is most likely it for GCJ10 for me this year, I’ll write up the analysis for R3, which will likely contain some interesting questions – they save the best for last…  Next stop is round 1 of TCO10.

GCJ10 R2

Didn’t quite make it through – was happy with my one solution, unhappy that I didn’t get any of the smalls out for the other 3, despite trying 2 of them for about an hour and a half.  I think the one I didn’t try looks like it was the easiest of course.

Final placement was 615th.  Will write up a full analysis after sleep…

TCO10 QR3

So I blinked and I missed the fact that qualifying round 3 had already happened.  I thought it was this coming weekend.  Having got through in QR1, forgetting about it is no drama, but it does mean my analysis of the problems is a bit late.

Q1) Given the top row and left column of a grid of numbers, where each number is supposed to satisfy the property of being equal to the sum of the 3 numbers to the bottom/right/bottom-right of it, determine the value of the bottom right number.  If the result cannot be uniquely determined return 0. Maximum grid size 10×10.

A1) First to get rid of the trivial cases.  If row length is 1, return last column entry.  If column length is 1, return last row entry.  Otherwise we actually have to fill in the grid.  This is pretty trivial since each cell is the sum of the 3 bottom/right/bottom-right, if you know the cell, the bottom and the right, you can subtract the bottom and the right to get the bottom-right cell.  Given you have the top and left sides, you can fill 1 cell immediately.  That cell lets you fill 2 more, and those, 3.  So you could diagonally fill the grid.  Or you could just fill row by row, or column by column.  In any case the cannot be uniquely determined scenario doesn’t exist so it can be ignored.

Q2) Given 6 numbers representing number of semitones to offset a guitar string (or -1 if that guitar string is not played), determine whether a major or minor cord is being played and if so which one.

A2) When I first started reading this question I had dreaded flashbacks to an old Top coder problem which was very similar.  However this one is relatively easy.  Modulo arithmetic comes into play.  Simply add the offsets (where not -1) to the numerical value of each guitar strings open note (as given in the problem for those who don’t play guitar) modulo 12.  Then reduce your (upto 6) notes to the unique values.  If you have more or less than 3 values you can return not-a-chord immediately.  Otherwise, sort the 3 values.  For each of the 3 values, check to see whether the next one (wrapping round if you start with the greatest value) is equal to the original + 4 or 3 modulo 12.  Then check if the remaining value is equal to the original + 7 modulo 12.  If both conditions are matched, return Major ‘letter’ by mapping the original value to the letters if the first condition was +4, or Minor ‘letter’ if the first condition was +3.  Otherwise return not-a-chord.

Q3) Given a pane of glass with width and height (both at most 1000), a starting position, and up to 2500 LRUD unit movements which cut the glass, starting from the starting position, how many pieces has the pane of glass been cut into?

A3) This is a simulation problem combined with a disjoint set enumeration.  The thing I find most annoying with problems like this is the data structure to store the results of the actions.  So used to a grid, storing details about edges is just annoying.  I usually end up storing the 4 edges of each cell and dealing with the fact that most edges belong to 2 cells by ensuring I always update both.  A grid of numbers can be used with numbers between 0-15 indicating which combination of surrounding edges have been cut, you can store these in bytes even making the grid require 1meg of ram.  Once the simulation is complete, calculating the pieces comes in to play.  I would do this with a disjoint set tracker (copying my code from TMD.Algo – since this is top coder… or maybe just writing it from scratch using arrays instead of dictionaries), creating a node for every square, then for each square unioning it with every neighbour which is on the other side of a non-cut edge.  Then for each square get the representative, and use a dictionary to accumulate the count of unique representatives.  Disjoint set tracker is practically O(1) for union and get operations, so even though there are a million cell locations the running time will not be a problem.  Memory usage will be a few megabytes, but within the 64meg limit.  This is really just showing my bias to disjoint set tracker, which I think is awesome.  For a similar approach which uses a tad less memory, you can just do floodfill counting using breadth or depth first searches which can’t pass the cut walls, which is also O(1) per grid cell.

Alternatively one could construct a graph out of the path followed and the edges of the puzzle.  Due to the limit on 2500 moves and 4000 edge positions, the memory usage will be much lower.  You can then walk the graph starting at each edge using a turn right at intersection approach, mapping each location visited (and handling dead-ends by turning around and keeping on walking).  Once you find a loop where you enter an edge from a direction other than the one you left it, you increment a counter and in any loop found case go back to starting at the next edge not already visited.  Once done the counter is the number of pieces. Each edge is visited at most three times, so O(N) in edges (moves + perimeter), which is far more efficient.  I would however consider it more complicated to implement, so I doubt I would have bothered.

All in all this looks like it was a pretty easy set.  Although being a qualifying round that is probably expected.

GCJ10 R1C

This round needed at least one problem out entirely, and if it wasn’t the third problem you needed the whole second problem or the small input for the third.  So it looks like it may not have been quite as easy as R1B.

Q1) Given n lines which all start and end with one of 2 x coordinates, and the list of y coordinates for each endpoint which are all distinct, and a guarantee that there will be no coincidence of intersections (no places where 3 or more lines cross), how many intersections are there. Constraint of n < 1000 for the big input.

A1) This is a simple O(N^2) problem, for each line it crosses another if the y coordinate order of one end is the opposite of the other.  I think I recall there being a way to do this faster… indeed the contest analysis mentions that this can be done in O(N log N), it is called the number of inversions in a permutation.  That is first sort the lines by their first end point, then determine how many pairs switch ordering when you sort by the second end point.  Apparently a merge sort can be adapted to perform this calculation, which is equal to the number of neighbour swaps required to sort them, which I have seen in previous problems which I have also solved using O(N^2) because the problem input size has never needed anything else.  I should probably look at adding this merge sort trick to TMD.Algo.

Q2) Given a known range which contains the maximum number of simultaneously connections a computer can handle (the upper end is known failure, lower end known success) and a requirement to reduce that range such that the top end is less than C times larger than the lower end (where C is an integer between  2 and 10, inclusive), what is the minimum number of tests required to guarantee that in the worst case.

A2) Bit of an odd question, one would first think a binary search would be idea, the size of the range would reduce consistently.  However, our target is not the size of the range but ratio of top to bottom.  Therefore we want a skewed binary search where the selection point is the one which results in A/S = S/B so our selection point is the sqrt(AB).  Now since the load tests can only be performed on integers, we’ll have to choose one of the 2 integers near the value of the square root.  In order to be ideal we must choose the one which has the best worst ratio, might as well use fractions just to be safe in comparing them. Then we simulate this process until the ratio is less than or equal to C.  The sample answer is not quite the same, however given that based on the selection point chosen, the ratio is sqrt(A/B) after n steps that is (A/B)^(1/(2^n)),  If we want that to be <=C then A/B <= C^(2^n), which is exactly the formula in the sample answer, so I think my approach is correct and should produce the required results.

Q3) Given a rectangle patterned with white and black squares, you need to make chessboards, starting with the largest chessboards (which can be larger than 8×8) all the way down to the smallest chessboards (1×1).  The largest chessboard available is always taken first, if there are multiple of equal size, top most and then left most are subsequent tie breakers.  Small problem set the input size is 32×32, large problem set the input size is 512×512.

A3) The small problem size isn’t too bad, we can simply brute force it, with a bit of caching to speed things up by a constant factor or 2.  The large problem size however is an entirely different story, an O(N^5) solution most certainly isn’t going to cut it.  Even an O(N^3) solution will likely fail to run in time.  This probably has something to do with the low success rate that this problem had.  So can the problem be done in O(N^2) ?

One trick is to construct the chessboards efficiently, every chessboard is itself a lot of smaller chessboards.  We require overlap to ensure consistency, so if you first identify all 2×2 chessboards. Those 2x2s can be used to create 3x3s.  3x3s can be used to create 4x4s and 5x5s, 4x4s can be used to create 6×6 and 7×7,5×5 can create 8×8 and 9×9.  If we try to race to the largest size and then work our way down filling the gaps in, this has potential. However each time we cut a chessboard out, we need to invalidate efficiently as well, all chessboards which overlap with the area need to be invalidated.  I still am not sure this is fast enough – it looks awfully like even with the best care and data structures it’ll be O(N^3) in the worst case.  Time to check out the sample solution I think…

So yes it can be done better than O(N^3), O(N^2 log N) in fact and the solution is very smart.  First of all we use dynamic programming to find the largest chessboard which has its top left corner positioned at that location.  I’ve seen this before but I’m so rusty I had clean forgotten it.  First check if it is the top left of a 2×2 chessboard, if not the answer is 1. Otherwise it is 1+ the minimum of the largest chessboard sizes for each of the other 3 locations in that 2×2 chessboard.  If we store each cell into a heap ordered by its largest size, y and x coordinates, we have the order to remove things in, the trick becomes the invalidation due to removal.  When you remove an area, every cell in that area now has a largest square of 0, but other cells may need their largest square being updated as well.  As it happens since we just removed the largest square, we know that no locations more than double that square away overlap.  So the number of locations to invalidate, recalculate and re-add to a heap is 4 times the number of locations removed, and since each cell can only be removed once, the number of invalidations is worst case 4*N^2.  Since each invalidation is a heap removal and addition, which is O(log N) each, we get O(N^2 log N)

All in all I suspect I would have placed in the top 300 in this round. top 400 in the previous. Even allowing for the fact I had a below average run in round 1A I probably would have only made top 400 in that round (maybe 250 on a good day).  So it doesn’t look good for getting top 500 and a t-shirt this year unless I do some serious improving.

GCJ10 R1B

Not sure whether attendance for 1B was higher or the problems easier, but the cut-off for getting through was a much higher score…  Unlike my round where solving any single problem with a decent time was sufficient, this round you needed at least 2.

Q1) Given a command for making new directories, which will only work if the parent directory exists, a list of current directories, and a list of directories required, determine how many command calls are required.  Constraints are up to 100 existing paths, 100 desired paths and each path at most 100 characters (and hence at most 50 deep)

A1) This problem makes R1A Q1 almost look tricky – just maintain a dictionary of existing paths and for each new path add/trim/add/trim until the add fails – and count each add.  The constraints are so small that the cost of generating alot of garbage strings isn’t even a problem – so no need for any kind of optimisation.  Do need to be careful you don’t try and add the empty string, but otherwise trivial.

Q2) Given a set of ‘points’ at different locations, and their velocities, and the position of some goal, and a restriction that in order for one point to pass another requires a ‘cost’ of 1, what is the minimum cost to have at least N points at the goal by time T.

A2) Okay, so this one is an actual problem… We can start by considering for each point, whether it can reach the goal based on its own velocity. If not we can kind of ignore those points.  If the number of points which can reach it based on own velocity is less than N, we can fail immediately.

At this point I would think that the right most N points which can reach the destination in time will be the ones we want.  Then the cost is the number of non-reachables to the right of each of those N points, as summed for each point.  If we were to choose a point which is more left, it would have to pass more non-reachables to get to the goal in time, increasing the cost.  This greedy approach seems sound, I’ll just check someone’s solution… yeap it is sound.  I now can see why you had to solve 2 problems to get through…

Q3) Given a set of numbers 2 to N, determine the number of subsets which contain N and its rank (ordered inclusive position 1-based), its rank’s rank, its rank’s rank’s rank, and so on until you get to the first element in the set.

A3) A math problem… and by far the hardest problem.  Constraints are N is 25 or smaller, or 500 or smaller for the large.  Quite a few got out the small constraint, but not so many for the large.

That being said, it isn’t really that hard of a problem.  Its a dynamic program over sets of length k, which end with m and satisfy the input constraint given m.  Both k and m are limited to being less than or equal to N, and the recursion is going to have to consider each shorter length, so O(N^3) is in play.  With N of 500, that is 125million steps, so each step itself better be quick.  So we’ll need a closer look to be sure.

Recursion shall be defined as the sum over each shorter length with m equal to the current k.  However each of those sum components need to be counted by the number of possible ways to choose the difference in lengths -1 extra numbers which are greater than k and less than m.  A direct approach results in that being O(difference in lengths) which is worst case O(N), which is nowhere near fast enough.  However all of the possible choose calculations can be precalculated in O(N^3) (or O(N^2) if you cache factorials and inverses mod p).  The important bit here is that the value of n choose k might be huge and the problem requires results modulo 100003.  Using TMD.Algo this will not be a problem, as I have an efficient n choose k mod p implementation already done and 100003 is prime (see, implementing stuff useful for last years GCJ, also useful for this year…).  Net result is a couple of hundred million multiplications  and additions and modulos.  The same lookup table can be used to answer all 100 inputs, so 8minutes is plenty of time.  In fact that last realisation is one I would have been at risk of missing during the contest, or I would have said I would have gotten all 3 problems out for sure if I was in this round.  Without reusing the table, the time constraint might be an issue, possibly depends on how fast your computer is… the look up tables will be close to fitting into cache on a good processor, but even then it might not be enough.

GCJ10 R1A

So, google code jam round 1A was today, and while I got through in 876th place (and so I won’t need to stay up for 1B or 1C), I feel the need to grumble a bit.  I started with the first problem, it was nice and simple (Especially considering I wrote AppleHunt…), I then went to the 3rd problem because I noticed that it was doable with the small input constraints and the constraints on the large input were the same as the small input.  By the time I had written my first non-functioning prototype for the solution, they had changed the constraints for the large output and my code wasn’t going to even come close to running in time. (and it would have required a couple of terrabytes of ram…)  I spent a few minutes working on the a different approach, since I had already done a bit of work on the problem, and the relaxed constraints actually suggested how the problem had to be solved. However, it quickly became apparent that I should go back to problem 2.  Another look at problem 2 and I was convinced that it was a simple dynamic programming problem.  However despite 3 submissions (including me noticing a blindingly stupid bug with <2min left on the clock) I wasn’t able to get something which solved the problem.  I have no clue why…  If I hadn’t of been mislead into to trying problem 3 first, I almost certainly would have either gotten problem 2 out, or at least submitted a solution for the small input size which used brute force rather than dynamic programming.

So I just had a look at a solution to problem 2 and I worked out why my solution failed, a subtle error in my thinking while constructing the dynamic programing resulted in it failing to consider the combination of 2 scenarios I had considered.  I also looked at problem 3 and have to say that the cool solution is excessively mathematical, but there is an option which performs reasonably which is more about programming…

So on to my analysis…

Q1) Given a board up to 50×50 with red/blue pieces on it – cause them to slide to form stacks coming left from the right hand side of the board.  Then determine for each of blue or red whether there exists k or more in a row either horizontally, vertically or diagonally.

A1)This problem is easily implemented in O(N^3) (where N is the width of the board) and that is actually sufficient, however I saw an O(N^2) solution and took it for the hell of it.  First step is to slide all the pieces – for each row you simply perform an order maintaining sort over empty-non-empty – or given that empty has no state, you maintain the last moved to position, move each piece to one left of that as you find them, either using actual moves, or simple assignments with a clear of all positions to the left of the top of the created stack afterwards.  O(N^2) for this step or up to O(N^3) if you ‘stable bubble sort’ the pieces in to position for each row.

Once the board is set, you have to determine who has ‘won’.  A simple walk of every square, and foreach walk in each of the 8 directions will determine this, or even only 4 directions.  This gives O(N^3).  I used DisjointSetTracker from TMD.Algo to create disjoint set collections for each of the 4 types of win.  For each square I unioned it with its win type neighbour set if they were the same colour – O(N^2).  I then determined the size of each disjoint set, using dictionaries to accumulate how frequently each representative member appears for every possible input square – O(N^2) – if the disjoint set size is greater than or equal to k, we look up who owns the representative member and they are a winner.

Q2) Given a sequence of numbers between 0 and 255 inclusive, and the ability to delete (for cost D), insert (for cost I), or change (for cost absolute difference between value before and value after) each number, determine the minimum cost to make every number have an absolute difference with its neighbours of less than or equal to M.

A2) The small problem constraints had a maximum of 3 inputs, something you could brute force.  However with up to 100 numbers, the large problem (which is the only one I actually attempted) required some other solution.  It was fairly obvious that you could define the problem recursively being the minimum cost to satisfy the first n numbers ending with value k.  And that is where I made my mistake.  I defined ending with value k to be after inserts were added, which is incorrect, it has to be ending with value k after modifying the value only.  With that correction we can proceed.  Cost for 1 element is 0, otherwise it is the minimum of satisfying scenarios with 1 less element.  The are 3 scenarios which need to be considered. Deletion, which is cost of the same final value with 1 less element + D. Change, which is the cost of each possible previous ending value which is within M of the current target value + the absolute difference between the current actual value and target value. Insertion, which is a bit more subtle – so long as M is non-zero it is the cost of each possible previous ending value + the I times the the difference between previous and target divided by M (using ceiling rather than floor to convert to integer) – I + Change cost.

Using dynamic programming you have a table of size 100*256 and a net cost of 100*256*256 since each entry requires an inner loop over previous values.  Apparently there is a way to calculate the table entries in average O(1) time each, but it is beyond my skill level.  On the other hand it actually starts with making the same mistake I did, but fixing it using some serious smarts.

Q3) Given 2 numbers A and B, you can subtract a multiple of the smaller from the larger (even if that multiple is 1) and end up with a non-negative number.  If we turn this into a game, where the aim is to not be left in a scenario where you are forced to end up with one of the numbers being 0, determine how many winning scenarios there are for you given all combinations of 2 input ranges for A and B (both being at most 1 to 1000000).

A3) The small input constrained the 2 ranges to being at most 30 wide, meaning only 900 games needing solving for each problem.  Given that I was looking at GCD in qualifying round for another problem I immediately recognised the potential for GCD to be related to this problem.  I determined that there is no difference between Solve(A, B) and Solve(A/GCD(A,B), B/GCD(A, B)), and I was going to use this to accelerate my solving of the problem. Assuming A>=B, A=B is a loss, A%B=0 is a win since you can leave B = B.  I then used GCD as an accelerator to reduce the number of subproblems to solve and Memoitzation.  Obviously I made a mistake as it didn’t produce the right results for the test inputs, but I think the approach would have been sufficient for the small input.  What I had failed to see was that there was a much better minimum condition for obvious win than A%B=0.  A >= 2B is a win.  This can easily be seen because either the smallest possible next non-zero value for A is a win, or it is a loss and smallest next value for A + B (which is <2B) will force force the opponent to create the small next non-zero value of A, B combination which you just found was a loss.  This is much more powerful and certainly puts the small solution easily within reach.

So what about the large problem set, where the ranges for A and B in inputs both can be up to 1 million wide?  Testing every possible scenario is … infeasible, so there obviously has to be some approach for determining whether a combination is a win or loss, for each possible value of A, in constant time for the entire range of the B inputs at once.  This is where I think the problem becomes a failure. If A >=2B is a win, then 2B>A>B is the interesting range for A. In this scenario we only have one possible move, which is to end up with B and A-B (which is now the smaller one).  If B>=2(A-B) it is now a win, which means the original position was a loss. Thus if A < 3B/2 the original position is a loss.  And we can keep recursing like this, chipping away at either side of the interesting range until it disappears.  That is okay and you can perform that recursion in log time – which while not constant time, is plenty good enough.  Where the problem fails is that this recursion can all be thrown away by a single comparison to the golden ratio, which is what the outside edges of the interesting range converge on, from above and below.  Why does that make the problem a failure?  Because its interesting enough that it has been solved before – so you have a number of people who will just pull the golden ratio out of nowhere and submit a solution, which is all math and requires no algorithms.

An alternative approach which is inspired from seeing that the interesting area can be recursively chipped away on the left and right, is that there is ultimately a single number which is the transition from win to lose.  You can then binary search to find that transition point, which is again fast enough, I think that solution ends up being O(N log^2 N) – maybe better with memotization…

R1B is tonight – I’ll do a write up for it tomorrow.

TCO10 QR2

So QR2 for TCO10 was yesterday, and I decided to take a look at the questions today.

Q1) Given a list of sell prices and buy prices for a specific type of object, and a tax on selling, determine maximum profit given you start with none of the item.

This is pretty easy.  Calculate the tax for each sale and reduce the sell price accordingly.  Then sort the adjusted sell prices descending and buy prices ascending, then while adjusted sell price is greater than the buy price, add the difference to the profit.

Q2) Given a square array of live, dead, unknown, determine the optimial values for unknown to ensure the maximum number of live cells after 1 round of the rules for Conway’s game of life.  Important restriction that no cell will have 2 or more unknowns in its set of neighbours.

Again this is pretty easy, al because of the restriction, but there are a few tricks.  First you need to put the the input array into a larger array so that you have a border (for easiest implementation a border of width 2), since cells may come to life outside of the input array.  Because of the restrction, every unknown can be considered seperately, which means the algorithm runs O(n) in cells. For each neighbour of an unknown, calculate whether it will be alive or dead for each of live and dead options for the unknown.  Do the same for the unknown itself. Chose the maximum sum of live cells for each of the two cases and set the unknown to that case.  Once all unknowns are set, simulate the entire board for one step and return the live count.

Q3) Given a paragraph (upto 1000 characters) with all the spaces and puncuation removed, and a list of up to 50 words (each up to 50 characters) determine a ’tiling’ of the paragraph using the words (each word may be used 0 or more times) which maximizes the value of the square of the longest consecutive tiled sequence minus the number of uncovered characters.

This is a big jump in difficulty and I have to say that I probably would not have gotten this one out.  It is fairly obviously dynamic programming, the problem comes in determining the right way to achieve it.  My first guess was to dynamic program over the first N characters, with the last M covered, but I realised I also needed have the largest consecutive cover P as a variable parameter too.  This meant that the dp space was length cubed, way too large.  After looking at another competitors solution I now see the correct approach.  Use dynamic programing to calculate the minimum number of uncovered tiles for a range starting at i and finishing at j.  This can be achieved by starting with the minum for a range of one less length + 1, and then minimizing by checking which words match at the end of the range and using the minimum for the range minus the matched word.  Note that the act of checking the words is quite expensive in the worst case, so pre caching whether each word matches for each location keeps the runtime under control.

Once we have the minimum number of uncovered tiles for each possible range, we loop over each possible range where that minimum is 0, square the range length and subtract the minimum number of uncovered tiles before and after the range. Return the maximum of all of those possible values and -length.  This works because although it doesn’t descriminate as to whether the range selected is the maximum consecutive uncovered section, when the maximum consecutive uncovered section is selected, it will receive a larger score because the number of uncovered tiles will be the same and the selected range length will be larger.

GCJ10 Qualifying Round

So the qualifying round is over and ~1500 people (including me ) got all the questions out successfully. 8523 people got the required 33 points to advance to the first round.

Questions were very mathematical, only the 3rd question really had any algorithmic design to it.

Q1) This was an amazingly convoluted way of asking when an n bit integer is all 1’s.  It boiled down to k % (1 << n) == (1<< n)-1, a single line.  I suspect the problem was inspired by reading Knuth.  He has a section where he describes the bit effect of adding 1 to a number (among other things…) the least significant 0 and all less significant bits toggle.  Which is the same as how a chain of ‘snappers’ was described to work.

Q2) This was a very mathematical question, with a barb to annoy anyone who’s programming language doesn’t include a big integer.  The question was given a set of up to 1000 numbers, what is the maximum common divisor you can get by increasing them all by the same a non-negative integer, and more specifically what is the minimum non-negative integer which gives that maximum common divisor.  The kicker being that the numbers were anything up to 10^50, so they won’t fit in a 64bit integer.

The solution comes from identifying an upper bound on the common divisor of n numbers is the GCD of the absolute differences between the numbers.  Then if the minimum number is a multiple of the GCD the rest of the numbers will be as well, which is always achievable for any set of numbers.  For .Net 4 the problem was trivial – BigInteger has a GCD method, so you just parse the numbers, calculate the absolute differences, calculate the gcd, calculate the mod of any entry with respect to the gcd, and if non-zero subtract that from the gcd to get the minimum shift.  GCD can be implemented using euclid algorithm if it wasn’t there.  And if you didn’t have biginteger, good luck…  My code had one change compared to the description above.  I sorted the entries rather than calculate absolute differences, but I’m pretty certain that isn’t required.

Q3) This problem had the longest implementation, but is mathematically simpler than problem 2.  The question was given a roller coaster with k seats and n groups of people of specific sizes in a specific order and the roller coaster does R runs per day and every paying customer is worth 1 dollar and everyone who went on the roller coaster goes back on the end of the queue in the same order they went on to the rollercoaster (no one ever leaves), how much money will the roller coaster operators get in a given day?

So it is a simple simulation problem, but R can be extremely large, making simulating all of them too expensive.  Also k*R can be larger than 32bit integer, so you need to be careful you don’t overflow using 32bits at inappropriate times.  The algorithmic key here is to identify that the simulation of a run is always the same for any given starting offset into the queue, so you only have to consider at most 1000 simulations (the maximum value for n).  The final piece is to realise that if you ever get back to the same starting person, you have started a loop and that sequence of simulations will be repeated.  The loop may not start from the first simulation, but a loop will exist eventually, the loop cannot be longer than 1000 entries.  So to calculate the answer you break it into 3 parts.  1) Simulate until loop discovered. 2) Calculate the sum of the loop and determine how many more times it needs to be run to get up to R (using integer division).  Calculate that product. 3) Calculate the modulo to determine the final partial loop and sum those steps.  Those 3 numbers are summed to give the answer.  Of course if R is too small you may not need step 2 or 3.

TCO10 QR1 Analysis

I’ll only cover the take 2 questions here, even though I thought the take 1 questions were better…

Q1) This question was a convoluted way of asking ‘what is the minimum number of neighbour swaps to sort a list containing 2 unique (but potentially repeated) elements’.  It was phrased a bit to confuse, but that is what it boiled down to.

I implemented this by hand writing a bubble sort and counting every swap, which was simple and for the input size, easily going to perform in time.  A more optimal solution (which takes advantage of there only being 2 entry types)  is to step through each entry, keeping a counter of how many of the type of element you want to be first you have seen so far.  Each time you encounter an element of that type, increase your result field by the difference between the current position and the number of elements of that type already seen.  This computes the result in O(N) rather than O(N^2). Oddly enough I swear I’ve seen this question somewhere else recently…

Q2) This one was a bit more tricky.  The question was, given a short (up to 10) list of UDLR direction commands, and a number of times to repeat them, and assuming every move is unit size, how many unique locations will you end up visiting?  The trick being the number of times to repeat them was up to 100 million…

The solution to this comes from realising that after the first few repetitions of the direction commands the number of new unique spots per repetition will always be the same.  I didn’t bother proving a minimum number of repetitions before it stabilises, which I suspect may be quite low, I just chose 20 to be on the safe side (current estimate is that 4 repetitions is sufficient for a command list maximum length of 10).  Use a set (dictionary since topcoder still only supports .Net 2.0) to store the locations you have visited, and simulate the the first few replications, storing how many new are added for every attempt. Then simply multiply the last number of new spots by the number of repetitions left, and add that to receive your final total.  I simplified my code by storing my location as a single integer rather than a pair, using 15 bits for one axis and the rest for the other.  Since the number of repetitions simulated is only 20 and the number of steps per reptition is 10, 15bits is overkill, but whatever…

Q3) This of course was the hardest question, and I didn’t get it completed in time. This question was a bit complicated, but it involved up to 50 different ‘lists’ being unioned together and sorted.  The lists themselves could be distinct values (stored in 50 characters, so at most 24 numbers) or a description of an arithmetic or geometric sequence.  The sequences were defined with start number, step/multiplier, number of elements in the sequence.  The program had to take this sorted union and return what element was at a given position in the list (for up to 50 positions).  Where that position was anything up to 1billion.  To simplify things a bit if the value at a position was over 1 billion, you were to return -1, and if the position asked was beyond the end of the sorted union, also return -1.  However to annoyingly complicate things, the input numbers were not gauranteed to fit in 64bit integers, although it was guaranteed that they would all be greater than 0.

Parsing the input was really much of the problem.  The restriction that values over 1billion return as -1 and positions beyond the end of the sorted union return -1 means that because it is sorted, you can ignore every value over 1billion and pretend it is not in the list.  Therefore int.TryParse and a check that the result if valid was less than or equal to 1 billion was sufficient for each field of the input.  Depending on which field what to do with the fact that the value is > 1billion you did have to treat it differently.  For the exact lists you could just throw each excessive element away.  For the sequences if it was the starting value, you could throw the entire sequence away.  If it was the multiplier/increment then you could replace the sequence with an exact list containing just one value.  However if it was a sequence length, you need to treat it as at least 1 billion.

Another side step which can be performed to reduce the problem space a bit is to realise that geometric sequence with a multiplier greater than 1 will all exceed 1 billion in 30 repetitions or less – so you can expand them into exact element lists.  With a multiplier of 1, you can replace them with an arithmetic sequence with step 0.  Therefore the rest of the code only has to handle arithmetic sequences and exact lists.  All of the exact lists can be combined and sorted so all that is left is a single exact list (might be empty) and up to 50 arithmetic sequences.  If you wanted all arithmetic sequences with length less than 30 or so could be treated as exact lists as well, but it doesn’t really help anything.

Finally now that the data is all prepared you have to find the actual values at given indexes.  This cannot be done by generating the first 1 billion entries, for one thing it would exceed the time limit, but for the other it most certainly would exceed the memory limit.  The solution comes from performing a binary search.  A binary search on what? A binary search on the ranges of positions which have a given value.  If the position we are after is in a range, it has the value for that range.  The function we need to implement the binary search is ‘how many numbers are less than n’.  We then binary search on n over the range 0 to 1billion asking the function for n and n+1. If the answer to the first question is greater than our required index, the answer must be smaller than n.  If the answer to the second question is less than or equal to our required index the answer must be greater than n.  Otherwise the answer is n.

Answering how many numbers are less than n for the exact list is trivial, since we sorted it.  You can even use a binary search, since the number of elements may be as high as 1500.  For the arithmetic sequences you can take n+1 subtract the start value, if result is negative return 0 else divide by the step size (using integer division) and add 1. Unless the step size is 0, which we introduced when getting rid of the  geometric sequences, in which case the answer is infinity (aka 1 billion) if n+1 is greater than or equal to the start value.  Whatever answer found has to be capped by the maximum number of entries for the sequence as defined previously. Finally the answer for each sequence and the exact list are summed, being careful not to overflow integer (since you could be adding the number 1 billion 50 times).  For an added performance kick this function can be memotized in case the different position queries end up passing in the same values.  It won’t make a huge difference to performance since the input values can be spaced such that only 5-6 out of up to 30 binary search steps will overlap for each query.

I ran out of time when I was just about to start implementing that final paragraph – but there was plenty of opportunity for off by one errors which I probably would have wasted a good 10 minutes fixing.

TCO QR1 Take 2

So the reschedule turned out to be 24 hours after the original time. So another extremely late night for me…
Results aren’t finalised yet, but I didn’t get round to submitting the 1000pt question which is still to be retested due to some bug in the writer’s solution… so my 139th position shouldn’t change much.
139th is probably not where I want to be, but its a start – I wasted a bunch of time deciding whether to go to bed or work on the 1000pt problem, was probably the difference between me getting a chance to make a submission or not. I had the right idea for the answer, but there was a lot of edge cases to screw up so even a submission might not have gotten me any further.
I’ll write up some analysis tonight I guess.