#P4050. Special Mahjong Waiting Hand
Special Mahjong Waiting Hand
Special Mahjong Waiting Hand
This problem involves a special variation of Mahjong. In this version, there is only one suit with tile numbers ranging from 1 to n (instead of the usual 1 to 9), and there is no limit of four copies per tile. A complete winning hand consists of 3m+2 tiles, where exactly two tiles form a pair and the remaining 3m tiles form m groups. Each group must be either a sequence (three consecutive numbers) or a triplet (three identical tiles).
You are given a hand of 3m+1 tiles. Your task is to determine whether the hand is a ready hand (i.e. it is waiting for one extra tile to form a complete winning hand). If it is, output all possible waiting tiles that, when added to the hand, will form a winning hand. If no such tile exists, output -1.
In the winning hand, a valid partition consists of one pair and m groups (each group being either a sequence or a triplet). A sequence is defined as three consecutive numbers (in \(a, a+1, a+2\)) and a triplet means three identical numbers.
inputFormat
The first line contains two integers n and m, where n is the maximum tile number and the hand is based on a single suit with numbers from 1 to n, and m is the number of groups in a complete hand.
The second line contains 3m+1 integers representing the tiles in the hand.
outputFormat
If there exists at least one tile that can complete the hand into a winning hand, output all such waiting tile values in increasing order separated by a space. If no such tile exists, output -1.
sample
9 1
2 3 4 5
2 5