#P7877. Spider Solitaire: Consolidation Moves and Card Move Counts
Spider Solitaire: Consolidation Moves and Card Move Counts
Spider Solitaire: Consolidation Moves and Card Move Counts
You are given an initial configuration of a simplified Spider Solitaire game. There are (m) horizontal piles of cards. All cards from (1) to (n) appear exactly once among the piles. Each pile is given in a left-to‐right order. In one move, you may choose a non‐empty pile and take a contiguous block of cards from its right end that forms a strictly descending sequence with a common difference of (-1) (i.e. if the block is (a_1, a_2, \dots, a_c), then (a_{i} - a_{i+1} = 1) for (1 \le i < c)). You may then append this block, in the same order, to the right end of another non‐empty pile if and only if the last card of that pile is exactly (a_1+1) (that is, one greater than the first card of the block).
The goal is to combine all (n) cards into one single pile such that the cards (from left to right) are in strictly descending order (i.e. (n, n-1, \dots, 1)). If it is possible to achieve this, output YES
and the minimum number of moves required; otherwise, output NO
.
Additionally, for every card (i) ((1 \le i \le n)), determine the minimum move number in which card (i) is moved (i.e. the first move during which card (i) is moved) over some winning sequence. If a card is never moved in any winning sequence then output -1
for that card.
Note: Any formulas are written in LaTeX format.
inputFormat
The first line contains two integers (m) and (n) representing the number of piles and the total number of cards respectively. The following (m) lines describe the piles. Each line starts with an integer (k) indicating the number of cards in that pile, followed by (k) integers representing the cards in that pile in left‐to‐right order. It is guaranteed that the (m) piles together contain all cards from (1) to (n) exactly once.
outputFormat
If it is possible to consolidate all cards into one pile meeting the goal condition, print a line containing YES
and the minimum number of moves needed (separated by a space). Otherwise, print a line containing NO
. In the next line, print (n) integers where the (i)th integer represents the minimum move number in which card (i) is moved in some winning sequence; if card (i) is never moved, output -1
for that card.
sample
3 5
2 5 3
1 4
2 2 1
YES 3
-1 2 1 3 -1
</p>