2300Card

2300   Card

题目描述

Xiaoxi wants to sort a hand of cards, but it is not as easy as usual.

Xiaoxi can only exchange two adjacent cards, or move one card to the start position or the end position.

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.
What is the smallest number of exchanges or moves required to sort a given hand of cards?

输入格式:

The first line contains an integer T to denote the number of data cases (T<=200).

The first line of input contains an integer n, the number of cards.

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 {s,h,d,c} representing the suits spades♠, hearts♥, diamonds♦, and clubs♣.

1 <= n <= 52

Σn <= 2000

There is no more than 10 testcases n > 10

输出格式:

For each testcase, print an integer respecting your answer in one line.

输入样例 复制
3
4
2h 9h 3c Ah
4
Ah 3h 6h 9h
4
9s 9h 9d 9c
输出样例 复制
1
1
0

说明

1
2
通过提交
时空限制3000ms/128mb
题目来源
评测方式在线评测
题目类型
难        度