Sort It Out
Time limit2sMemory limit512 MB
Find the smallest subset of cow IDs S such that repeatedly yelling at each ID in S in increasing order eventually sorts the permutation, then output the K-th lexicographically smallest such subset.
- Level
Hard8 of 10
- Topics
- Sorting, Combinatorics, Greedy, Brute force
- Solved
- No attempts yet
Problem
FJ has () cows, distinctly identified , lined up in a row. FJ likes his cows to be sorted in increasing order, but unfortunately they are currently out of order. In the past FJ used algorithms such as "bubble sort" to sort his cows, but today he feels quite lazy. Instead he will yell at one specific cow at a time to "sort it out". When yelled at, a cow makes sure she is not out of order from her point of view. While there is a cow immediately to her right with a smaller ID, the two swap places. Then, while there is a cow immediately to her left with a larger ID, the two swap places. Finally the cow is done "sorting it out", at which point the cow to her left has a smaller ID and the cow to her right has a larger ID.
FJ wants to pick a subset of cows, then iterate through this subset yelling at each of them in turn in increasing order of ID, again and again until all cows are sorted. For instance, if he picks the subset of cows with IDs , he will yell at cow , then cow , then cow . If the cows are still not sorted, he will yell at these same cows again, as many times as necessary.
Since FJ is not sure which cows are paying attention, he wants to minimize the size of this subset. FJ also thinks the number is very lucky. Help him find the -th lexicographically smallest subset of minimal size such that shouting at its cows repeatedly will eventually result in all cows being sorted.
A subset of is lexicographically smaller than a subset if the list of elements of in increasing order is lexicographically smaller than the list of elements of in increasing order. For instance, is lexicographically smaller than .
Input
The first line contains two integers, and (). The second line contains space-separated integers, the cows' numbers from left to right.
It is guaranteed that there are at least valid subsets.
Output
The first line of output contains the size of the minimal subset. The remaining lines contain the IDs of the cows in the -th lexicographically smallest subset of minimal size, one ID per line, listed in increasing order.
Hint
We start with the array . After FJ yells at the cow with ID 1, the array becomes . When FJ yells at the cow with ID 4, the array becomes . At this point the array is sorted.