BifidSortMachine
SRM 306 · 2006-06-08 · by soul-net
Problem Statement
We have a sorting machine that works on a list of distinct numbers. This machine only has two instructions, named MOVEBACK and MOVEFRONT. Each instruction takes one element of the list as a parameter and removes that element from the list. MOVEBACK will then append that element to the end of the remaining list, while MOVEFRONT will insert it at the beginning.
For example, the sequence {8,12,25,7,15,19} can be sorted in ascending order using 2 instructions:
MOVEFRONT 7, to get {7,8,12,25,15,19}
MOVEBACK 25, to get {7,8,12,15,19,25}
You will be given a
Constraints
- a will have between 1 and 50 elements, inclusive.
- Each element of a will be between -1000 and 1000, inclusive.
- All elements of a will be distinct.
{8,12,25,7,15,19}
Returns: 2
The example from the problem statement.
{1,2,3,4,5}
Returns: 0
This list is already sorted, so no instructions are needed.
{1000,-1000,0}
Returns: 1
This list can be sorted with a single instruction: MOVEBACK 1000.
{6,5,4,3,2,1}
Returns: 5
{2,3,4,5,9,6,7,8,1}
Returns: 2
Submissions are judged against all 128 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BifidSortMachine with a public method int countMoves(vector<int> a) · 128 test cases · 2 s / 256 MB per case