ShufflingCardsDiv2
SRM 641 · 2014-08-25 · by ltaravilse
Problem Statement
Ciel always uses the same procedure when shuffling. One round of shuffling looks as follows:
- She splits the deck into two piles: the top N cards will be pile A, the bottom N cards pile B.
- She takes pile A and rearranges the cards it contains arbitrarily.
- She takes pile B and rearranges the cards it contains arbitrarily.
- She interleaves the cards from the two piles, producing a single deck again. More precisely, if pile A has cards A1,A2,...,AN and pile B has cards B1,B2,...,BN then the new deck will be A1,B1,A2,B2,...,AN,BN. (Note that the first card has to come from pile A.)
- She splits the deck into two piles: the cards 1,2 are pile A and the cards 3,4 are pile B.
- She rearranges pile A into 1,2. (I.e., she keeps the cards in their current order.)
- She rearranges pile B into 4,3.
- She merges the two piles, obtaining the deck 1,4,2,3.
- 1,3,2,4
- 1,4,2,3
- 2,3,1,4
- 2,4,1,3
Constraints
- permutation will contain between 4 and 200 elements, inclusive.
- The number of elements in permutation will be even.
- The elements of permutation will form a permutation of the numbers 1 through 2N, where 2N is the number of elements in permutation.
{1,2,3,4}
Returns: "Possible"
Fox Ciel can make the following two shuffles: {1,2,3,4} -> {1,3,2,4} -> {1,2,3,4}. Note that she cannot simply keep the deck in sorted order, the shuffling procedure does not allow that. Luckily for Ciel, it is possible to shuffle the deck in the first round and to return the cards to their original places in the second round.
{4,3,2,1}
Returns: "Possible"
{1,3,2,4}
Returns: "Impossible"
Ciel can produce this permutation after the first round of shuffling. However, it is not possible to start with a sorted deck and to have this permutation of cards after two rounds of shuffling.
{1,4,2,5,3,6}
Returns: "Impossible"
{8,5,4,9,1,7,6,10,3,2}
Returns: "Possible"
Submissions are judged against all 90 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ShufflingCardsDiv2 with a public method string shuffle(vector<int> permutation) · 90 test cases · 2 s / 256 MB per case