Prob Cards
Prob Cards
3 4 4 2 2 1
5 3 1 2
5 2
43 24 12
35
5 21
2
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 were sold out in less
than two hours! While waiting for the games to start, you decide to do something fun to keep your
mind off the wait: play your favourite card game.
The card deck contains four standard suits of different colours: silver (S), white (W), emerald (E),
and red (R), as well as one trump suit coloured cyan (C). That is, the Cyan cards outrank all other
cards. There are N cards of each suit, numbered from 1 to N. This means that, in total, the deck
comprises 5N cards. At the beginning of the game, the deck is randomly distributed between the five
players, such that each player gets N cards.
Before you start playing, you want to organise your cards such that all cards of the same suit are
next to each other in increasing order, and the trump cards appear at the end (also in increasing order).
When you receive your cards, they appear in your hand as a sequence. To organise them, you perform
a sequence of actions, where in each action you take one card out of your hand and put it back in your
hand at another position (between two cards, before the first card, or after the last card).
You cannot help but wonder: what is the minimum number of actions you need to take in order to
organise your hand?
Input
The input consists of two lines. The first line contains the number N. The second line contains N
space-separated values describing the sequence of cards in your hand. Each value is composed of one
letter of the set {S, W, E, R, C} (describing the card suit), followed by an integer V such that 1 ⩽ V ⩽ N
(describing the card number).
Output
The output should contain a single line, consisting of a single number: the minimum number of
actions required to organise your hand.
Limits
• 1 ⩽ N ⩽ 100 000.
Sample Input 1
4
C1 R2 E4 R1
Sample Output 1
2
Sample Input 2
5
S2 W4 E1 R5 C1
Sample Output 2
0