## CCC '12 S4 - A Coin Game

View as PDF

Points: 15
Time limit: 3.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### Canadian Computing Competition: 2012 Stage 1, Junior #5, Senior #4

When she is bored, Jo Coder likes to play the following game with coins on a table. She takes a set of distinct coins and lines them up in a row. For example, let us say that she has a penny (P, worth $0.01), a nickel (N, worth$0.05), and a dime (D, worth $0.10). She lines them up in an arbitrary order, (for example, D N P), and then moves them around with the goal of placing them in strictly increasing order by value, that is P N D (i.e.,$0.01, $0.05,$0.10). She has particular rules that she follows:

• The initial coin line-up defines all positions where coins can be placed. That is, no additional positions can be added later, and even if one of the positions does not have a coin on it at some point, the position still exists.
• The game consists of a sequence of moves and in each move Jo moves a coin from one position to an adjacent position.
• The coins can be stacked, and in a move Jo always takes the top coin from one stack and moves it to the top of another stack.
• In a stack of coins, Jo never places a higher-value coin on top of a lower-value coin.

For simplicity, let the coins have consecutive integer values (e.g., denote the penny as 1, nickel as 2, and dime as 3). Then, in the above example, Jo could play the game in the following way in moves (where denotes that coin is on top of coin ):

MovePosition 1Position 2Position 3
initial 321
1 3 12
2 13 2
3 13 2
4 3 1 2
5 3 12
6 3 12
7 13 2
8 1 3 2
9 1 23
10 123
MovePosition 1Position 2Position 3
11 23 1
12 2 3 1
13 2 13
14 12 3
15 12 3
16 2 1 3
17 2 13
18 2 13
19 12 3
20 1 2 3

For some starting configurations, it is not always possible to obtain the goal of strictly increasing order.

#### Input Specification

The input will contain some number of test cases. A test case consists of two lines. The first line contains a positive integer (), which is the number of coins. We assume that the coins are labeled . The second line contains a list of numbers to in an arbitrary order, which represents the initial coin configuration. For the above example, the input test case would be:

3
3 2 1

The end of test cases is indicated by on a line by itself.

#### Output Specification

For each test case, output one line, which will either contain the minimal number of moves in which Jo can achieve the goal coin line-up, or, if it is not possible to achieve the goal coin line-up, IMPOSSIBLE.

#### Sample Input

3
3 2 1
2
2 1
0

#### Output for Sample Input

20
IMPOSSIBLE

• commented on Dec. 6, 2020, 8:01 p.m.

I TLE on cases 4 and 5, not sure why... My code runs fine and I get the 15 points on the official CCC judge...

• commented on Nov. 25, 2017, 12:37 p.m.

Ehhhhh, I think the numbers are a bit messed up..... and I keep TLE... help?

• commented on Nov. 25, 2017, 12:44 p.m.

Doesn't overlap for me. It's probably because of your mac laptop.

• commented on Nov. 24, 2017, 5:42 p.m.

The tables are overlapping...

• commented on Nov. 25, 2017, 2:52 p.m.

Tables are overlapping for Mac.

• commented on Feb. 1, 2017, 9:45 p.m.

I don't think my solution should pass
I TLE on:

...gg problem...