ENG  RUSTimus Online Judge
Online Judge
Online contests
About Online Judge
Frequently asked questions
Site news
Problem set
Submit solution
Judge status
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests

Open Ural FU Personal Contest 2012

About     Problems     Submit solution     Judge status     Standings
Contest is over

C. Space Rummy

Time limit: 1.0 second
Memory limit: 64 MB
Fashion in card games is capricious. Just two years ago no one knew anything about God forsaken planet Texas and game “rummy” which Texans love so much. Today you’ll hardly find a casino in the Galaxy where people don’t dice away their fortune at this simple game.
Galaxy Committee on card games set out the following rules for rummy. The game is played with two decks of n cards, all the cards of different kinds, numbered from one to n. In the beginning, each of the two players takes a full deck and shuffles it thoroughly. The deck is laid on the table face down. Then both players show their upper cards at the same time. If the cards are of the same kind, both cards are put away and aren’t used in the game anymore. In the other case the player whose card was of a greater kind, takes both cards and puts them into the bottom of his deck so that the greater card is at the very bottom and the minor is right atop of it. Then the players show the upper cards of their decks again. The player loses if he is out of cards. If the players are out of cards at the same time, it’s a draw.
Card-sharper Dustin has recently put a tiny scanner in his eye, which lets him see the order of the cards in the partner’s deck. Now he wants to shuffle his cards in such a way that guarantees him a victory. Of course, if Dustin puts his cards in the same order as his partner’s, he’ll easily get the draw. But that’s not his cup of tea, because he only needs victory.


The first line contains an integer n (2 ≤ n ≤ 500) that is the number of cards in each player’s deck. The second line contains a permutation of numbers from 1 to n denoting kinds of cards in Dustin’s partner’s deck in top-down order.


Output the word “YES” in the first line, if Dustin can shuffle his cards in the order which provides him a victory. In the second line output the sought order of cards in Dustin’s deck. If Dustin cannot provide a victory, output a single word “NO”.


2 3 4 1
2 4 1 3
Problem Author: Andrey Demidov
Problem Source: Open Ural FU Personal Contest 2012
To submit the solution for this problem go to the Problem set: 1943. Space Rummy