Connection Status:
Competition Arena > ShufflingCardsDiv2
SRM 641 · 2014-08-25 · by ltaravilse · Math
Class Name: ShufflingCardsDiv2
Return Type: String
Method Name: shuffle
Arg Types: (vector<int>)
Problem Statement

Problem Statement

Fox Ciel likes shuffling cards. She uses a deck with 2N cards, numbered 1 through 2N.
Ciel always uses the same procedure when shuffling. One round of shuffling looks as follows:
  1. She splits the deck into two piles: the top N cards will be pile A, the bottom N cards pile B.
  2. She takes pile A and rearranges the cards it contains arbitrarily.
  3. She takes pile B and rearranges the cards it contains arbitrarily.
  4. 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.)
For example, let N=2 and suppose that Ciel starts with the sorted deck 1,2,3,4. One possible round of shuffling looks as follows:
  1. She splits the deck into two piles: the cards 1,2 are pile A and the cards 3,4 are pile B.
  2. She rearranges pile A into 1,2. (I.e., she keeps the cards in their current order.)
  3. She rearranges pile B into 4,3.
  4. She merges the two piles, obtaining the deck 1,4,2,3.
In the above example we have shown one of four possible outcomes of the shuffling process. After the first round of shuffling, Ciel could have that deck in one of these four orders:
  • 1,3,2,4
  • 1,4,2,3
  • 2,3,1,4
  • 2,4,1,3
You are given a int[] permutation which contains a permutation of the 2N cards. Ciel's deck is currently sorted: the cards are in the order 1,2,3,...,2N from top to bottom. Ciel wants to make exactly two rounds of shuffling. After the second round the order of cards in her deck should correspond to the given permutation. Return "Possible" (quotes for clarity) if this can be done and "Impossible" otherwise.

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.
Examples
0)
{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.

1)
{4,3,2,1}
Returns: "Possible"
2)
{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.

3)
{1,4,2,5,3,6}
Returns: "Impossible"
4)
{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.

Coding Area

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

Submitting as anonymous