Editorial for COCI '09 Contest 2 #2 Rimski


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.

Let us examine all possible cases that require modification of the input sequence. We can easily see that such cases will end up producing 4 or 9 on their ones place, and 40 on the tens place. All numbers ending with 6 (6 itself, 16, 26, 36, 46, etc) can be transformed into the corresponding number ending with 4, such as 6 (VI) to 4 (IV), 16 (XVI) to 14 (XIV) etc. You might have noticed at this point that one number, 66, will not transform into 64. We will account for that later on. Also, some, but not all, numbers ending with 1 can be transformed to end with 9. For example, 11 (XI) transforms into 9 (IX), 21 (XXI) into 19 (XIX), 31 (XXXI) into 29 (XXIX), 41 (XLI) does NOT transform into 39 however, neither does 51 (LI) into 49 (IL). The correct way to write 49 would be (XLIX). 61 (LXI) can be transformed into 59 (LIX) so can 71 into 69 and 81 into 71. 91 (CXI) cannot transform into 89 (LXXXIX). Now we have covered all cases that require displacement of ones. We just need to account for numbers between 60 (LX) and 69 (LXIX). These numbers can be transformed into 40 – 49 range by displacing the first X before L. Note that we will now cover the special case of 66 and 71. 66 (LXVI) can be first transformed into 46 (XLVI) and then into 44 (XLIV). 71 (LXXI) can transform into 69 (LXIX) but 69 can transform into 49 (XLIX).

Of course, instead of going through all possible special cases, you can count all available characters and construct the smallest possible number.


Comments

There are no comments at the moment.