Programming contests

ACM ICPC programozó csapatverseny, házi forduló, 2017. október 29.

October 29, 2017 10:00 AM – October 29, 2017 3:00 PM

Card Hand Sorting


Sample Input 2

When dealt cards in the card game Plump, it is a good idea to start by sorting the cards in hand by suit and rank. The different suits should be grouped, and the ranks should be sorted within each suit. But the order of the suits does not matter, and within each suit, the cards may be sorted in either ascending or descending order on rank. It is allowed for some suits to be sorted in ascending order and others in descending order.

Sorting is done by moving one card at a time from its current position to a new position in the hand, at the start, end, or in between two adjacent cards. What is the smallest number of moves required to sort a given hand of cards?

Input Specification

The first line of the input contains an integer n (1 ≤ n ≤ 52), the number of cards in the hand. The second line contains n pairwise distinct space-separated cards, each represented by two characters. The first character of a card represents the rank and is either a digit from 2 to 9 or one of the letters T, J, Q, K, and A, representing Ten, Jack, Queen, King, and Ace, respectively, given here in increasing order. The second character of a card is from the set {shdc}, representing the suits spades ♠, hearts ♡, diamonds ♢, and clubs ♣.

Output Specification

Output the minimum number of card moves required to sort the hand as described above.

Sample Input 1

  1. 4
  2. 2h Th 8c Qh
download as text file

Output for Sample Input 1

  1. 1
download as text file

Sample Input 2

  1. 7
  2. 9d As 2s Qd 2c Jd 8h
download as text file

Output for Sample Input 2

  1. 2
download as text file

Sample Input 3

  1. 4
  2. 2h 3h 9c 8c
download as text file

Output for Sample Input 3

  1. 0
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019