Published using Google Docs
Solutions to #1 15-295 Fall 2020
Updated automatically every 5 minutes

15295 Spring 2020 #1 -- Problem Discussion

September 2, 2020

        

This is where we collectively describe algorithms for these problems.  To see the problem statements follow this link.  To see the scoreboard, go to this page and select this contest.

A. Latin Squares

We can directly check if the number of distinct elements in each row/column is n (in O(n^2) time), and if the top row and leftmost column are sorted, which is enough information to determine the answer.

(You can wrap your solution with while(!cin.eof()){} in c++ to keep running until the end of input.)

B. Unloaded Die

If we are changing a face with probability p, the amount we will have to change it by is simply |3.5-(current EV)|/p. Thus taking the maximum p gives us our answer.

C. Halfway

Let f(x) be the number of comparisons that the program can make with only having x be the largest number it’s printed so far. We see this is (n-1) + … + (n-x) = x * (2n-x-1)/2. So the question becomes what is the smallest x such that f(x) is at least half the comparisons. We see that f(x) is an increasing function, so we can binary search! Since we can compute f(x) in O(1), this is only O(log n) so we’re good.

D. Purple Rain

Let’s construct an array whose value is 1 if the corresponding region is ‘R’ and -1 if the corresponding region is ‘B’. Now, the difference between the number of ‘R’s and the number of ‘B’s in a subarray of the original array is equal to the sum of the corresponding subarray in the new array.

Now, the problem reduces to finding the maximum possible absolute value of a subarray sum of this new array. We can achieve this by finding both the max subarray sum and the min subarray sum using dynamic programming.  [This is pretty vague.  Can you flesh it out with a DP recurrence or something? ---DS]

The DP used here for maximum subarray sum is commonly known as Kadane’s algorithm.

Let dp1[i] = maximum sum for a subarray ending at index i. Our recurrence here would be dp1[i] = max(0,dp1[i-1])+a[i], where a is our original array with 1, -1. Similar logic follows for minimum subarray sum: dp2[i] = min(0,dp2[i-1])+a[i], where dp2[i] represents the minimum sum for a subarray ending at i.

E. Crusher’s Code

In both algorithms, the number of inversions (pairs i, j such that A[min(i, j)] > A[max(i, j)]) do not increase after each iteration of the loop - they stay the same if the “if” statement is not entered, and decrease by at least one if it is entered (in that case, we make progress). When the array is sorted at the end, the number of inversions is 0.

Denote the expected number of iterations from array state S by I_S, and denote the new state we reach from state S after swapping i and j by swap(S, i, j). Also let P_S be the set of pairs (i, j) for which we make progress when we run Monty’s algorithm on state S. Then we have the following formula for the expected number of iterations from some array state S for Monty’s algorithm.

I_S = 1 + (sum over P_S of I_{swap(S, i, j)}) / n^2 + (1 - |P_S| / n^2) * I_S

The first 1 is the current iteration, the second term covers the cases where we make progress, and the last step covers cases where we don’t and we end up with the same array one iteration later. The base case, where S is sorted, has I_S = 0.

This simplifies to I_S = (n^2 + sum over P_S of I_{swap(S, i, j)}) / |P_S|

By memoizing the computations over the possible values of S, we can get the answer in O(n! * (n^2)) time, which is ok since n is at most 8. We can do a similar thing for Carlo’s algorithm - instead of iterating over pairs (i, j), we instead iterate through possible values i (which will potentially swap i and i + 1), and get a slightly different formula accordingly; the time complexity for this is slightly less - it is O(n! * n), so the overall time complexity is dominated by the one to compute the expected iterations of Monty’s algorithm, which is sufficient.

-- Ram

F. Bones’s Battery

Observe that, if the condition of the problem is possible for some shuttle range X, then any value larger than it is also possible. In addition, if a value X does not work, then any smaller value will also not work. (In other words, a smaller value is strictly “harder” than a larger one.) Thus, this problem can be approached with a binary search for the answer. So, we will try to determine for any X whether it is possible to travel from each node to each other node in K or fewer recharges with a capacity of X.

It remains how to check whether a value X is possible. To do this, we will determine the shortest number of recharges necessary from a node i to all other nodes. If any of these values are greater than K, then this value of X is impossible. We can then iterate over all nodes and determine for each node if it is possible.

We can use a slightly modified version of Dijkstra’s Algorithm. Instead of using a single value as the distance to each node, we will use a pair, storing both the number of recharges needed, as well as the amount of distance traveled in the current charge. If a road would cause you to exceed X, then you should have recharged before traveling on that road.

With this algorithm, we can determine the number of recharges (and additional distance on the final recharge, but that’s not relevant for the checking) needed to travel to each node from the starting node i. Therefore, we can determine if a shuttle range of X is possible, and our binary search will determine the minimum working shuttle range.

The complexity of this solution is O(log(MAX)*N*N^2) per test case (MAX is the maximum possible distance, which can be bounded by N*max_d=1e11). (This runs in about 2.5s on CF servers, although it’s a bit too close for comfort :p)

-------

We can also use Floyd Warshall to solve this question. It computes all pairwise shortest paths in O(n^3).

  1. We first run Floyd Warshall using the given set of edges to determine their distance.
  2. Do a binary search on the minimum range.
  3. In each binary search iteration, check if range m works as follows:
  1. Construct a new set of edges by: for every pair {X,Y} of nodes, if dist[X][Y] <= m, connect the pair by adding an edge (X,Y,w=1) to the new set.
  2. Run Floyd Warshall again on the new set of edges.
  3. Check for every pair {X,Y}, if newDist[X][Y] > K, return false;
  4. Else return true, meaning all pairwise distances (stops) are <=K.

This has a run time of O(log(MAX)n^3).

-Jin