Under construction

Still being built. Things may change

Card Game

Time limit2sMemory limit1024 MB

Summary
Split a shuffled deck of n cards, k of them face down, into two piles with equal face-down counts using Reverse, Move, and Up actions.
Level

Hard9 of 10

Topics
Math, Implementation, Simulation
Solved
No attempts yet

Problem

This is an interactive problem.

Tony Stark likes to play cards with Vision in the free time between battles. Ordinary card games got boring fast, so the two decided to invent their own.

The deck has nn cards numbered from 11 to nn. All of them lie face down on the table. Before the game, Vision shuffles the deck thoroughly and, without Tony knowing, turns over kk arbitrary cards so that their backs face down. Tony's task is to split the deck into two non-empty piles so that both piles contain the same number of face-down cards. He must do this without looking at the deck. Tony can perform these actions:

  • Reverse: flip the top card of the deck.
  • Move: move the top card of the deck into a separate pile.
  • Up i: move the card at position ii from the top to the top of the deck. The top i−1i-1 cards shift down.
  • Finish: announce that no more actions will be performed on the deck. This message means that the remaining deck and the separate pile contain the same number of face-down cards.

After each action, Vision (the judge) reports how many face-down cards are in the separate pile.

Hint

The following is one possible sequence of events in the sample.

  • The deck has 6 cards, 4 of them face down.
  • Move the top card to the separate pile. It was face down, so the judge reports 1 face-down card in the separate pile. The deck now has 3 face-down cards.
  • Reverse the new top card.
  • Move it to the separate pile. It now lies face up, so the returned value does not change. Before the flip, the top card was face down, so the deck has 2 face-down cards.
  • Move the fourth card (the last one in the current deck) to the top.
  • Reverse it.
  • Move it to the separate pile. The judge reports that it is face down. Now both the deck and the separate pile have 2 face-down cards, so Tony can finish. He wins.

Examples1

  1. Example 1

    Input
    6 4
    1
    1
    1
    1
    1
    2
    
    Expected output
    Move
    Reverse
    Move
    Up 4
    Reverse
    Move
    Finish