Card game
InterviewTime limit1sMemory limit1024 MB
Find the fewest single-card moves to group the hand by suit with each suit sorted by number, and put the cyan cards last in increasing order.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Binary search, Array
- Solved
- No attempts yet
Statement
The Olympic Games in Paris are almost here, and you are thrilled! You and four of your friends managed to get five of the rare tickets for sports climbing, even though all tickets sold out in less than two hours. While you wait for the games to start, you decide to play your favourite card game.
The card deck has four standard suits in different colours: silver (S), white (W), emerald (E), and red (R). It also has one trump suit, cyan (C). Cyan cards outrank all other cards. Each suit has cards numbered from to , so the deck has cards in total. At the start of the game, the deck is dealt randomly among five players, with cards each.
Before you play, you want to organise your hand. Cards of the same suit must be next to each other in increasing order, and the trump cards must come last, also in increasing order. The cards arrive as a sequence. In one action, you take one card out of your hand and put it back at another position: between two cards, before the first card, or after the last card.
Find the minimum number of actions needed to organise your hand.
Input
The input has two lines. The first line contains . The second line contains space-separated values. Each value is one letter from {S, W, E, R, C} for the suit, followed by an integer with for the card number.
Output
Print a single line with the minimum number of actions.