Editorial for Yet Another Contest 9 P1 - Permutation Cutting
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
If there is an index with , then these two elements must clearly belong to different subsequences. On the other hand, it always suffices to cut the permutation in between these indexes, since the subsequences formed will each be contiguous subsequences of , and thus can be rearranged to form . So the answer is plus the number of such indexes.
Time complexity:
Subtask 2
With the same logic, the answer is plus the number of indices where is not a contiguous subsequence of . This can be calculated fast by storing the adjacent pairs of elements of in a set, or storing the index of each element in .
Time complexity: or
Comments