#P4907. Minimum Card Exchanges for Consecutive Suit Sequence

    ID: 18148 Type: Default 1000ms 256MiB

Minimum Card Exchanges for Consecutive Suit Sequence

Minimum Card Exchanges for Consecutive Suit Sequence

Little Ben is about to play cards but is extremely disturbed by the disorder in his hand. Although he already groups his cards by suit, his obsessive nature demands that within each suit the cards must form a consecutive sequence in the natural order: \(A-2-3-\dots-10-J-Q-K\). To achieve this, he is allowed to exchange some of his cards with other players; however, the exchange rule is very strict: he can only exchange cards with the same rank (for example, a card showing 7 with another card showing 7, regardless of suit).

Given his current hand, he wants to exchange as few cards as possible so that after the exchange, for every suit present in his hand the cards (when sorted in increasing order by their numerical value) form a consecutive sequence, i.e. they can be arranged as a block of ranks in the order \(A,2,3,\dots,K\). Note that a card's rank is fixed and cannot be changed, but its suit may be swapped if an exchange is made with another card of the same rank. Your task is to compute the minimum number of cards in his hand that need to be exchanged to achieve his goal.

Explanation: Assume the hand contains cards from up to four suits (denoted by C, D, H, and S) and each card is given by a suit and a rank. Ranks are given as A, 2, 3, …, 10, J, Q, K (which correspond to the numerical values 1,2,…,13 respectively). For a given suit, let \(k\) be the number of cards of that suit. The goal is to assign these cards a target contiguous block of ranks (of length \(k\), for example, [r, r+1, \(\dots\), r+k-1] with \(1 \le r \le 14-k\)). You may keep a card if its rank already lies in the chosen block; otherwise, that card must be exchanged. Since an exchange involves trading a card with another card of the same rank from a different suit, the process is reciprocal. However, we count the number of cards in Little Ben’s hand that have to be exchanged. In other words, if the maximum total number of cards that can be kept (over all suits) is \(T\) and the total number of cards in the hand is \(N\), then the answer is \(N - T\).

Input constraints: The first line contains an integer \(N\) (the total number of cards in the hand). Each of the following \(N\) lines contains a card description with two tokens: a suit (one of C, D, H, S) and a rank (one of A, 2, 3, …, 10, J, Q, K). It is guaranteed that \(1 \le N \le 52\). All input values are separated by spaces or newlines.

Time Limit: 1 second

inputFormat

The first line contains an integer \(N\), the number of cards in the hand.

Each of the next \(N\) lines contains a suit and a rank, separated by a space. The suit is one of C, D, H, or S. The rank is one of A, 2, 3, …, 10, J, Q, or K.

outputFormat

Output a single integer: the minimum number of cards that need to be exchanged so that in each suit the cards can be arranged in a consecutive sequence (in the order \(A-2-3-\dots-10-J-Q-K\)).

sample

8
C A
C 4
D 3
D 2
H A
H 2
S A
S 3
2