Card Shuffle

Chef is organizing an online card game tournament and for this purpose he has to provide a card shuffling software. This software has to simulate the following shuffling process. A stack of N cards is placed face down on the table. Cards in the stack are ordered by value. Topmost card has value 1 and the one on the bottom has value N. To shuffle the cards we repeat the following steps M times:

  1. take A cards from the top of the deck.
  2. take another B cards from the top of the deck.
  3. put the A cards, which you removed in the first step, back on top of the remaining deck.
  4. take C cards from the deck
  5. put the B cards, which you’re still holding from the second move, card by card on top of the deck.
  6. finally, return the block of C cards on top

Note: taking a block of cards from the top of the deck does not change their order. The entire block is removed in a single move and not card by card. The only exception is the fifth move, where you return cards one by one from the top.

Input

The first line contains integers N and M. The following M lines describe the moves by integers Ai, Bi, Ci as described in the previous section.

In the spirit of random card shuffling, all test cases were generated with uniform random distributions to select where to cut the deck of cards.

Output

In a single line output the cards in the deck after performing all the moves. Cards should be listed from top of the deck to bottom and separated by spaces.

Constraints

  • 1 <= N, M <= 100 000

Example

Input:

10 2

6 2 2

5 3 6

Output:

1 2 8 7 3 9 6 5 4 10

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int numberOfCards = 10;
            int A = 6, B = 2, C = 2;
            //Initializing the card stack
            ArrayList cardStack = new ArrayList();
            for (int i = 0; i < numberOfCards; i++)
            {
                cardStack.Add((int.Parse((i + 1).ToString())));
            }
            Console.WriteLine("Initial Card Sequence: ");
            for (int i = 0; i < cardStack.Count; i++)
            {
                Console.Write(cardStack[i] + " ");
            }
            Console.WriteLine();

            //Shuffle
            cardStack = Shuffle(cardStack, A, B, C);
            A = 5; B = 3; C = 6;
            cardStack = Shuffle(cardStack, A, B, C);
            Console.WriteLine("Sequence After Shuffling: ");
            //Printing the cards from top to bottom
            for (int i = 0; i < cardStack.Count; i++)
            {
                Console.Write(cardStack[i] + " ");
            }
            Console.WriteLine();
        }
        public static ArrayList Shuffle(ArrayList cardStack, int A, int B, int C)
        {
            //Taking A cards from the top of deck
            Array tempCardStack1 = Array.CreateInstance(typeof(int), A);
            cardStack.CopyTo(0, tempCardStack1, 0, A);
            cardStack.RemoveRange(0, A);
            //Taking B cards from the top of deck
            Array tempCardStack2 = Array.CreateInstance(typeof(int), B);
            cardStack.CopyTo(0, tempCardStack2, 0, B);
            cardStack.RemoveRange(0, B);
            //Inserting A cards from tempCardStack1 back to the deck
            for (int i = A - 1; i >= 0; i--)
            {
                cardStack.Insert(0, tempCardStack1.GetValue(i));
            }
            //Taking C cards from the top of deck
            tempCardStack1 = Array.CreateInstance(typeof(int), C);
            cardStack.CopyTo(0, tempCardStack1, 0, C);
            cardStack.RemoveRange(0, C);
            //Inserting B cards from tempCardStack2 back to the deck individually
            for (int i = 0; i < B; i++)
            {
                cardStack.Insert(0, tempCardStack2.GetValue(i));
            }
            //Inserting C cards from tempCardStack1 back to the deck
            for (int i = C - 1; i >= 0; i--)
            {
                cardStack.Insert(0, tempCardStack1.GetValue(i));
            }
            return cardStack;
        }
    }
}
Output:
=======
Initial Card Sequence:
1 2 3 4 5 6 7 8 9 10
Sequence After Shuffling:
1 2 8 7 3 9 6 5 4 10
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s