luogu#P4907. A换B problem

A换B problem

Background

The fairy and Xiao Ben made a deal: to get the magic medicine, he must beat her in a card game.

Problem Description

They are about to start playing cards. However, Xiao Ben, who has obsessive-compulsive tendencies, feels very unhappy when looking at the cards in his hand. The reason is that he sorts cards by suit (are you kidding me), and he also hopes that the cards of the same suit are consecutive (A2345678910JQKA-2-3-4-5-6-7-8-9-10-J-Q-K).

Having had enough, Xiao Ben starts swapping cards with the other players (cheating???). He wants to swap as few cards as possible to meet his requirement (assume any card can be swapped).

Swap rule: you can only swap cards with the same rank.

Everyone: Can you just play properly?! We give you 1 second to finish swapping, or we will tell the fairy!

Input Format

The first line contains a positive integer NN, meaning Xiao Ben has NN cards.

The next NN lines each contain two positive integers, representing the suit and the rank of a card.

Output Format

There are two lines in total:

The first line is a judgment: if it can be completed, output Yes, otherwise output No.

The second line outputs the minimum number of swaps, or the minimum number of cards needed.

10
1 A
1 2
1 4
2 3
2 5
2 6
3 9
3 10
3 Q
4 J
Yes
2
10
1 A
1 2
1 4
2 3
2 5
2 6
3 9
3 10
3 Q
4 K
Yes
2

Hint

Sample 1: achievable. Swap 22 33 to 11 33, and swap 44 JJ to 33 JJ.

Sample 2: achievable. Swap 22 33 to 11 33, and swap 33 QQ to 44 QQ. (The above answers are for reference only. There may be multiple solutions, but the output is definitely unique. Do not ask me why.)

For 10%10\% of the testdata: it is sample 22.

For 100%100\% of the testdata: N52N \leq 52, meaning the data is guaranteed to be within one deck of cards.

Translated by ChatGPT 5