## Editorial for Wesley's Anger Contest 4 Problem 3 - Squirrel Tag

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

First of all, note that since Michael moves faster than all of the squirrels, a solution is guaranteed to exist (and hence some optimal solution exists).

##### Subtask 1

In this subtask, all squirrels remain on the x-axis during the game of tag. The "intended" greedy solution involves trying to catch all the squirrels to the left of the origin before all the squirrels to the right, and vice versa, but sadly I cannot prove its correctness (with the exception of proof by AC).

**Time Complexity:**

**Remark:** For the next two subtasks, note that any optimal solution will involve catching the squirrels in some order. There can be several such orders, but no matter what, Michael will inevitably have to catch the squirrels in some order.

Now let's convince ourselves that an optimal solution is always found if Michael catches each squirrel as quickly as possible before the next squirrel in this order. Let an optimal solution be such that Michael catches the squirrels in the order and catches each squirrel at the respective point at times . Now suppose that in this optimal solution, Michael is capable of catching some squirrel at some time , and he can catch it at the point . Then, since Michael runs faster than all the squirrels, he can run to before (since , on its normal course, would be there at ), and we see that a solution in which Michael catches each squirrel as soon as possible is *at least as good* as this optimal solution, and thus must be optimal itself.

##### Subtask 2

For subtask 2, with constraints , we have bounds small enough to permit checking every possible order: all of them. Then, it only remains to find how much time it takes to travel from squirrel to squirrel, which can be done via intermediate math or binary search, left as an exercise.

**Time Complexity:** or ^{[1]}

##### Subtask 3

Subtask 3 was simply the improvement from factorial to exponential time, the well-known travelling salesman problem. The most popular solution is to represent each state as a pair of the most recently tagged squirrel and a bitmask representing which squirrels have been previously tagged. Constraints were made to permit binary search to pass (since this isn't a math competition, after all).

**Time Complexity:** or ^{[1]}

^{[1]} Note that is slightly misleading since you'll be binary searching decimal numbers, so you'll require somewhat more iterations to achieve the required precision, but it's a fair ballpark.

## Comments