Editorial for Yet Another Contest 4 P5 - Signpost
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Let's consider two signs with positions and . The relative order of these signs on the signposts is solely determined by whether is greater than, less than, or equal to . This motivates us to consider the set containing all values of where and are the positions of two different signs. Then, it is clear that the only values of that we need to consider are the elements of set , since for any other value of , we could move the signpost to the first element of which is greater or lower than without restricting the signposts that we can make.
Consider a fixed value of . Let be the number of unordered pairs of signs such that the average of their positions is equal to . Then, the number of signposts that can be made is , since each of the pairs can be ordered either way, and the positions of all other signposts on the signpost are fixed.
At first glance, the answer to the problem appears to be . However, it can be seen that for any two adjacent elements of , there is exactly one sign that can be made at both positions. Hence, the answer is .
For ease of implementation, we can work with only integers if we instead define as the number of unordered pairs of signs such that the sum of their positions is equal to . Then, the answer is . For subtask 1, it suffices to calculate naively by looping over all pairs of signs.
Time complexity:
Subtask 2
Let be if there is a sign at position , and otherwise. Then, it can be seen that . Thus, we can efficiently calculate using FFT.
Time complexity:
Comments