It is a necessary and sufficient condition that we have exactly 2 distinct values for x and y. If we have less than 2 distinct values for any variable, then there is no way to know the length of that dimension. If there are at least 3 distinct values for any variable, then that means more than 3 vertices lie on that dimension, which cannot happen since there can be at most 2 vertices in a line segment. The area, if it can be found, is just the difference of values of the x coordinates times the difference of values of the y coordinates.
No matter what, we make |b 1| operations to make a 1 equal to b 1. Once this is done, a 2, a 3, ... a n = b 1. Then no matter what, we must make |b 2 - b 1| operations to make a 2 equal to b 2. In general, to make a i = b i we need to make |b i - b i - 1| operations, so in total we make |b 1| + |b 2 - b 1| + |b 3 - b 2| + ... + |b n - b n - 1| operations.
Note that if there is an integer d so that the number of w i equal to d differs from the number of the given squares whose weight equals d, then the answer is automatically "NO". This can be easily checked by using a map for the w i and the weights of the squares and checking if the maps are the same. This step takes time.
Let d be an integer, and let D be the set of all i so that w i = d. Let W be the set of all special points (x, y) so that the weight of (x, y) is d. Note that W and D have the same number of elements. Suppose that i 1 < i 2 < ... < i k are the elements of D. Let (a, b) < (c, d) if a < c or a = c and b < d. Suppose that (x 1, y 1) < (x 2, y 2) < ... < (x k, y k) are the elements of W. Note that the point (x j, y j) has to be labeled by i j for 1 ≤ j ≤ k.
Now, each special point is labeled. It remains to check if this is a valid labeling. This can be done by taking an array of vectors. The vector arr[i] will denote the points with x-coordinate i. This vector can be easily made from the points given in O(n) time, and since the points are already labeled, arr[i][j] will denote the label for the point (i, j). Now, for all points (i, j), the point (i, j + 1) (if it is special) and the point (i + 1, j) (if it is special) must have a greater number than (i, j). This step takes a total of O(n) time.
Bonus: Can you do this problem in O(n) time?
Comments: This problem was inspired by the representation theory of the group of permutations S n (Representation theory of the Symmetric Group). Essential objects in the study of S n are Young diagrams and standard Young tableau (Young Tableau). The weight of a point as defined by the problem is basically the same thing as the content of a square in a standard Young tableaux. If you have questions, feel free to message me.
Let us solve this problem using dynamic programming.
First let us reindex the trees by sorting them by x-coordinate. Let f(i, j, b 1, b 2) where we would like to consider the problem of if we only have trees i... j standing where b 1 = 1 indicates that tree i - 1 falls right and b 1 = 0 if it falls left and b 2 = 1 indicates that tree j + 1 falls right and b 2 = 0 if it falls left.
We start with the case that Wilbur chooses the left tree and it falls right. The plan is to calculate the expected length in this scenario and multiply by the chance of this case occurring, which is . We can easily calculate what is the farthest right tree that falls as a result of this and call it w i.
Then if w i > = j this means the entire segment falls, from which the length of the ground covered by trees in i... j can be calculated. However, be careful when b 2 = 0, as there may be overlapping covered regions when the tree j falls right but the tree j + 1 falls left.
If only w i < j, then we just consider adding the length of ground covered by trees i... w i falling right and add to the value of the subproblem f(w i + 1, j, 1, b 2).
There is another interesting case where Wilbur chooses the left tree and it falls left. In this case we calculate the expected length and multiply by the chance of this occurring, which is . The expected length of ground covered by the trees here is just the length contributed by tree i falling left, which we must be careful calculating as there might be overlapping covered regions with the i th tree falling left and the i - 1 th tree falling right. Then we also add the value of subproblem f(i + 1, j, 0, b 2).
Doing this naively would take O(n 3) time, but this can be lowered to O(n 2) by precalculating what happens when tree i falls left or right.
We should also consider the cases that Wilbur chooses the right tree, but these cases are analogous by symmetry.
Complexity: O(n 2)
Solution 1: Suppose that s is a string in the query. Reverse s and the direction of all the moves that can be made on the table. Note that starting at any point that is part of a cycle, there is a loop and then edges that go out of the loop. So, for every point, it can be checked by dfs whether the s can be made by starting at that point by storing what is in the cycle.
Moreover, note that in the reversed graph, each point can only be a part of one cycle. Therefore, the total time for the dfs in a query is O(nm·SIGMA + |s|). This is good enough for q queries to run in time.
Complexity: where SIGMA = 10 is the number of distinct characters in the table, and s i is the query string for the i th query.
Solution 2 (Actually too slow, see comment by waterfalls below for more details): For each string s, dfs from every node that has in degree equal to 0 in the original graph. There will be a path which leads into a cycle after which anything in the cycle can be used any number of times in s. Only every node with in degree equal to 0 has to be checked because every path which leads to a cycle is part of a larger path which starts with a vertex of in degree 0 that leads into a cycle.
This solution is slower, but it works in practice since it is really hard for a string to match so many times in the table. Each query will take O(n 2·m 2 + s i) time, but it is much faster in practice.
Complexity: where SIGMA = 10 is the number of distinct characters in the table, and s i is the query string of the i th query.