QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 1024 MB Puntuación total: 100

#4761. 手牌排序

Estadísticas

在纸牌游戏 Plump 中,发牌后最好先按花色和点数对牌进行排序。不同的花色应该归为一组,并且每种花色内的牌应按点数排序。花色的顺序没有要求,而在每种花色内,牌可以按点数升序或降序排列。允许某些花色按升序排列,而其他花色按降序排列。

排序是通过一次移动一张牌来完成的,即将一张牌从当前位置移动到手中的新位置(牌堆的开头、末尾或任意两张相邻的牌之间)。对给定的手牌进行排序所需的最少移动次数是多少?

Sample Input 2 (cc by-sa NCPC 2016)

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 52$),表示手中的牌数。

第二行包含 $n$ 张两两不同的、以空格分隔的牌,每张牌由两个字符表示。第一个字符表示点数,是 $2$ 到 $9$ 之间的数字,或者是字母 $T, J, Q, K, A$ 中的一个,分别代表 Ten(10)、Jack(J)、Queen(Q)、King(K)和 Ace(A),按递增顺序排列。第二个字符来自集合 $\{s, h, d, c\}$,分别代表黑桃 ♠、红桃 ♥、方块 ♦ 和梅花 ♣。

输出格式

输出将手牌按上述规则排序所需的最少移动次数。

样例

输入格式 1

4
2h Th 8c Qh

输出格式 1

1

输入格式 2

7
9d As 2s Qd 2c Jd 8h

输出格式 2

2

输入格式 3

4
2h 3h 9c 8c

输出格式 3

0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.