Summary:
Sort the cats according to the metric v / x in descending order, where v is the cat's constant speed and x is the cat's initial displacement at time t = 0. It doesn't matter how you break ties. Once the order is initially established, it will remain the most efficient order in which to get cats as long as it is followed; so follow it.
Candidates debunked:
In both cases, allow the motorcycle speed to be w = 20.
It is proposed that you get cats in order from fastest to slowest. Counterexample: Cat #1 (x, v) = (1, 9) and Cat #2 (x, v) = (100, 10).
It is proposed that you get cats in order from closest to farthest. Counterexample: Cat #1 (x, v) = (1, 1) and Cat #2 (x, v) = (2, 100).
Detailed Derivation:
Let c(k) refer to the kth cat the lady picks up, v(k) refer to the speed of that cat and x(k) to the cat's initial displacement (at time t = 0, which we set a the time the lady starts her motorcycle initially in pursuit of the first cat).
The total time taken to get the first cat is:
t(1) = 2 * x(1) / (w - v(1))
where w is the constant speed of the motorcycle. Since this expression is going to be important we can motivate every part of it:
2 *
comes from the fact that the lady must catch the cat, and then spend the same amount of time to return the cat home;
x(1) / (w - v(1))
is the time taken to reach the cat, that is, close the distance x(1)
by traveling w - v(1)
faster than the cat's v(1)
.
The time to get the first two cats is:
t(2) = t(1) + 2 * (x(2) + v(2)t(1)) / (w - v(2))
That is, it takes time equal to the time to get the first cat plus the time to get the second cat. The extra v(2)t(1)
term accounts for the fact that the second cat moves while the lady is getting the first cat; otherwise, this part is the same.
Rearranging this expression, we get:
t(2) = t(1)(1 + 2 * v(2) / (w - v(2))) + 2 * x(2) / (w - v(2))
We define the following derivative terms:
T(k) = 2 * x(k) / (w - v(k))
s(k) = 2 * v(k) / (w - v(k)) + 1
Now we rewrite:
t(1) = T(1)
t(2) = s(2)T(1) + T(2)
and continue
t(1) = T(1)
t(2) = s(2)T(1) + T(2)
t(3) = s(3)s(2)t(1) + s(3)T(2) + T(3)
...
t(n) = s(n)...s(2)T(1) + s(n)...s(3)T(2) + ... + T(n)
This last expression gives us the total time to get all n
cats:
s(n)...s(2)T(1) + s(n)...s(3)T(2) + ... + T(n)
Now we assume that we have an optimal solution in that the cats are picked up in the most efficient order possible. To derive useful properties about this hypothetical optimal solution, we can use the supposed optimality to infer that swapping cats produces a solution that is no better. Imagine swapping cats j
and j+1
:
... + s(n)...s(j+1)T(j) + s(n)...s(j+2)T(j+1) + ...
<= ... + s(n)...s(j)T(j+1) + s(n)...s(j+2)T(j) + ...
Terms involving T(k)
for k < j
have both s(j)
and s(j+1)
and by the commutativity of multiplication they are unaffected by the swap. Terms involving T(k)
for k > j + 1
have neither s(j)
nor s(j+1)
and so cannot be affected by the swap. Only the terms with T(k)
such that j <= k <= j + 1
are affected by the swap, so we can remove like terms:
s(n)...s(j+2)s(j+1)T(j) + s(n)...s(j+2)T(j+1)
<= s(n)...s(j+2)s(j)T(j+1) + s(n)...s(j+2)T(j)
The partial product s(n)...s(j+2)
is common to all remaining terms and must be positive, so we can remove this like term by dividing both sides of the inequality:
s(j+1)T(j) + T(j+1) <= s(j)T(j+1) + T(j)
Rearrange this as follows:
(s(j+1) - 1)T(j) <= (s(j) - 1)T(j+1)
Finally:
(s(j+1) - 1) / T(j+1) <= (s(j) - 1) / T(j)
Recalling our definitions of s(k)
and T(k)
, simplify to put this in terms of v
and x
:
v(j+1) / x(j+1) <= v(j) / x(j)
That is: if we have an optimal solution, it must be the case that the ratio of cats' speeds to initial displacements must be in descending order. This is a necessary, but perhaps not sufficient, condition.
Note that this result agrees with intuition:
- Get still cats last (v = 0)
- Get cats that haven't left yet first (x = 0)
- Get cats approaching the motorcycle's speed first (or never)
- Get cats that are really far away last (x -> +inf)
It also gives the correct result for the two-cat case; and in that case, if the ratios of speed to displacement are equal, then it can be easily shown that it doesn't matter which order you get the cats in (if they are unequal, you must get the cat with the higher ratio first).
Now - I have not addressed the case where cats may have the same ratio. It's not immediately obvious to me that the order in which you get cats with the same ratio doesn't matter.
However, suppose you have chosen optimally up until some point k < n
. Now you need to decide which of two cats with the same ratio to go after. As we already mentioned, for the two-cat problem, it's a wash: so I think the answer is that it can't matter which one you choose, as either order among the two will take the same time and "look" the same afterwards. To see that two cats that start with the same ratio keep the same ratio:
v(i) / x(i) = c; X(i) = x(i) + v(i)t = x(i) + x(i)ct = x(i)(1 + ct)
v(j) / x(j) = c; X(j) = x(j) + v(j)t = x(j) + x(j)ct = x(j)(1 + ct)
So the ratio changes over time (if you take X
as the new initial displacement) but two cats that start out with the same ratio will keep it. The new ratio will be:
v / x = c; v / X = v / x(1 + ct) = c / (1 + ct)
It is important to note that these ratios don't "cross over" each other either; if you start out with a higher or lower ratio, it will change over time, but it will not become higher or lower than other cats' ratios:
c(i) / (1 + c(i)t) > c(j) / (1 + c(j)t)
<=> c(i) + c(i)c(j)t > c(j) + c(i)c(j)t
<=> c(i) > c(j)
Based on all of these considerations, my best answer is:
Sort the cats according to the metric v / x in descending order. It doesn't matter how you break ties. Get the cats in that order.