SortingSubsets
SRM 701 · 2016-10-02 · by Arterm
SRM 701 · 2016-10-02 · by Arterm · Graph Theory, Math, Recursion, Simple Search, Iteration
Problem Statement
Problem Statement
You are given a
You want to rearrange the elements of a into a non-decreasing order. What is the smallest possible number of elements you have to move?
Formally, the operation looks as follows:
- You select some set of positions in a.
- You permute the elements on the chosen positions arbitrarily.
Constraints
- a will contain between 1 and 50 elements, inclusive.
- Each element of a will be between 1 and 100, inclusive.
Examples
0)
{52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52}
Returns: 0
1)
{96, 96, 96, 34, 96, 34, 34, 34, 96}
Returns: 6
2)
{91, 37, 37, 37, 37, 91, 91, 29, 37, 91, 37, 29, 29, 37, 91, 29, 91, 37, 37, 29, 37, 91, 91, 91, 29, 29, 37, 37, 37, 91, 29, 37, 37, 91, 29, 91, 29}
Returns: 28
3)
{11, 11, 49, 7, 11, 11, 7, 7, 11, 49, 11}
Returns: 7
4)
{90, 95, 95, 73, 95, 21, 89, 90, 73, 21, 95, 90, 73, 95, 21, 89, 89, 73, 95, 73, 90, 90, 21, 73, 73, 95, 89, 90, 73, 89, 90, 89, 90, 89, 89, 73, 95, 89, 90}
Returns: 32
20)
{3, 2, 1}
Returns: 2
One can take the first and the last element and swap them.
21)
{1, 2, 3, 4}
Returns: 0
The array is already sorted, so we can select an empty set of positions.
22)
{4, 4, 4, 3, 3, 3}
Returns: 6
Here all elements must be taken and permuted.
Submissions are judged against all 27 archived test cases, of which 8 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class SortingSubsets with a public method int getMinimalSize(vector<int> a) · 27 test cases · 2 s / 256 MB per case