SortArray
TCO19 SRM 761 · 2019-06-21 · by misof
Problem Statement
Given is an
The algorithm is a sequence of instructions described by the
For example, suppose we have the array A = {30, 40, 70, 20, 10} and we receive the command C = 11 = 2^0 + 2^1 + 2^3. This command rearranges the values A[0], A[1], and A[3] into non-descending order. Thus, after the command the new array will be A = {20, 30, 70, 40, 10}.
If the given sequence of commands correctly sorts any array of length N, return an empty
Constraints
- N will be between 1 and 15, inclusive.
- commands will contain between 0 and 200 instructions, inclusive.
- Each element of commands will be between 1 and 2^N-1, inclusive.
7
{127}
Returns: { }
The only instruction is "sort the entire array". This program obviously works correctly.
8
{85, 170}
Returns: {0, 1, 2, 3, 4, 5, 7, 6 }
The first instruction sorts the elements at even indices, the second instruction sorts the elements at odd indices. This clearly isn't sufficient. The return value happens to be the lexicographically smallest counterexample, but any other permutation that does not get sorted correctly would be accepted as well.
3
{3, 5, 6}
Returns: { }
Sorting three elements using three pairwise comparisons.
10
{255, 1020}
Returns: {3, 4, 5, 6, 7, 8, 9, 0, 1, 2 }
"Sort elements A[0..7] and then sort elements A[2..9]." This is not a valid sort yet. The permutation given as the example return value would first be rearranged to {0, 3, 4, 5, 6, 7, 8, 9, 1, 2 } and then to {0, 3, 1, 2, 4, 5, 6, 7, 8, 9}.
10
{255, 1020, 255, 1020, 255, 1020, 255, 1020, 255, 1020}
Returns: { }
... but doing the same thing multiple times in a row seems to work.
15
{ 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767 }
Returns: { }
The largest possible input, in some sense: a 15-element array and 200 instructions to sort the whole thing.
Submissions are judged against all 99 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SortArray with a public method vector<int> verify(int N, vector<int> commands) · 99 test cases · 2 s / 256 MB per case