Editorial for IOI '08 P3 - Fish
Submitting an official solution before solving the problem yourself is a bannable offence.
Observe that the order in which the fish eat each other is irrelevant. All that matters is whether the jewels of a given set of fish can be "united" into one fish (through the eating process described in the problem statement). We can arrive at the following lemma:
Lemma 1
The jewels that you get might be those of a given set of fish if and only if the longest fish in is at least twice as long as the second longest fish in .
Proof
If the longest fish in cannot eat the second longest, their jewels can never be united unless a fish not in is involved. If the longest fish is at least twice as long as the second longest, then the longest one can eat everyone else in successively.
The tricky part of this problem is to avoid counting some possible combination of jewels more than once. One way to avoid this is to map every possible combination of jewels to the longest fish that can have that combination in its stomach. We then attempt to count, for each fish, the number of combinations mapped to it.
Note: For simplicity we will say that a fish, which was originally given a jewel of kind , is itself "a fish of kind ".
Lemma 2
Unless a fish is the longest one of its kind, it will have no combinations mapped to it.
Proof
If we have a fish of kind and a longer fish also of kind , then whatever can be eaten by can also be eaten by . So any jewel set that's mapped to can also involve the longer fish instead of , which is a contradiction with the mapping method defined above.
Lemma 3
If the longest fish of kind can eat fish of kind and another fish of kind can eat more than fish of kind , then no combinations that contain any jewel would be mapped to the longest fish of kind . (Observe that in such a case the fish must necessarily be longer than the fish .)
Proof
This is a similar case to the one above. Any such set that's mapped to the longest fish of kind can also be found in the stomach of the longer fish of kind , leading to another contradiction.
Lemma 4
If the longest fish of kind can eat fish of kind and another longer fish of kind can also eat , but not fish of kind , then the only combinations that can potentially be mapped to the longest fish of kind would be ones which either have jewels or no jewels .
Proof
Again, using the superset principle we find out that if a combination has at most jewels of kind and at least one jewel of kind , then it can be found in the stomach of the longer fish of kind and thus cannot be mapped to a fish of kind .
Knowing this, we can build an algorithm to tell us which combinations are mapped to a given fish.
Note: For simplicity we will denote by the number of fish of kind that can be eaten by the longest fish of kind .
Following lemma 2, we're only interested in the longest fish of their respective kind. For each such fish of kind , we count two types of combinations that are mapped to it. First, combinations that have the maximum number of jewels of kind (which is ). These are called the "full" combinations. Second, all other combinations mapped to . These are called the "partial" combinations.
Now, for every other kind , with longest fish , we count how many jewels of kind can be part of a "full" or a "partial" combination mapped to . The above lemmas give rise to three scenarios:
- If is more than , meaning that can eat more fish of kind than , then no jewels of kind can be part of any combination mapped to .
- If is longer than , but doesn't fall into the above category, there can be no jewels of kind in the "partial" category of , but there can be anywhere between and jewels in the "full" category.
- If is shorter than , then any number between and of fish of kind can participate in either "full" or "partial" combinations of .
Except for , the number of jewels of any two colors are independent of each other (i.e., the first count doesn't influence the feasibility of the second count in any way and vice versa).
A naïve implementation of this algorithm gives running time since for each longest fish of its kind, we look through all other fish and determine the values. This should be sufficient for 56 points.
We can improve the naïve implementation if we realize that we need only one loop over all the individual fish, as long as the fish are sorted by length. Then we can go from smallest to longest and count how many fish of each kind we have seen so far. We will use to denote how many fish of kind we have seen. Then when we reach the largest fish that can be eaten by a fish of kind , we have the values for in the current values of . Implementing this properly yields an algorithm with the bottleneck being the sorting of the fish and the bottleneck being the evaluation of the products of the numbers for each jewel kind. This solution is sufficient for 70~75 points.
We now work towards an solution, which gets 100 points for this problem. The key observation here is that if we have the kinds sorted by the length of their respective longest fish, when we compute the number of combinations mapped to a given kind , all we do is add together a few numbers, each of which is a product of some consecutive numbers (where "consecutive" refers to ).
This means that we can achieve an solution by keeping the array (which at given times becomes for each kind ) in a data structure that allows us to modify the array in time and to extract the product of a continuous section of the array in time as well. This can be done using a binary tree data structure with the leaves of the tree storing the numbers and each node in the tree storing the product of the numbers in its sub-tree. A primitive illustration of this (with the leaves on top) would be as follows:
Note: indicates that the node is keeping the product of .
Clearly, each change in the array affects the value stored in of these nodes, hence any updates to the data structure can be achieved in time by combining the values of the node's children in every affected node, going from the affected leaf to the root. It can also be shown that any interval of the array can be decomposed into at most of these intervals, so the product of values in any interval can be calculated in time as well.
Final Note: The author also developed an extended version of this problem in which you catch fish, rather than just , and the jewels from the two fish are counted together. It is also doable in polynomial time, although it is much more complicated. If you are enthusiastic about solving this version, you should feel free to send your solutions to [email protected].
Comments