Editorial for WC '18 Contest 4 J4 - Your Name, Please


Remember to use this editorial 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.

N+1 button presses are required just to confirm a length-N name, regardless of its letters (an A button press upon selecting each letter, plus a final + button press at the end). Therefore, if K < N+1, then no valid name exists. Otherwise, let B = K-(N+1) be the number of additional </> button presses required.

Let's consider how many such button presses each letter in a name contributes. Let m(p, x) be the number of button presses required to move the cursor to a letter x from the previous letter p (with the previous letter before the first one assumed to be A). We'll either repeatedly press < or >, whichever requires fewer presses. These two options require |x-p| and 26-|x-p| button presses (in some order), giving us m(p, x) = \min(|x-p|, 26-|x-p|). We can observe that, given any previous letter p, it's possible to choose some next letter x such that m(p, x) has any wanted value between 0 and 13, inclusive.

We can then observe that a valid name exists if and only if B \le 13N. Furthermore, we can construct a name for a given B value by greedily choosing letters one by one such that m(p, x) is maximized each time (up to at most 13), but without exceeding the remaining allotment of required button presses.


Comments

There are no comments at the moment.