Polish version    English version  
  History of OI -> II OI 1994/1995 -> Problems

 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
III OI 1995/1996
II OI 1994/1995
Stage III - results
Stage I - results
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
Niebieskie ksi.eczki
II Olympiad in Informatics 1994/1995

Task: POC
Author: Andrzej Walat

III stage contest  

On a chute on a port wharf there are barrels in three colours: red, green and blue lying in random order. One is to put them in order so that the red barrels are in front (the lowest), then the blue ones follow, and the green ones are in the end (the highest).

One may use a crane to arrange the barrels. The crane - in one move - can lift any three adjacent barrels from the chute and move them up. Then the barrels lying above roll down filling the gap and the crane places at the upper end of the chute the barrels taken.

One is to schedule the crane's work dictating in the consecutive moves which triple of barrels should be moved up to the end.

The arrangement of barrels on the chute is represented by a sequence of l letters c, n, z, where l is the number of barrels. The letters c, n, z represent a red ("czerwona" in Polish), blue ("niebieska") and green ("zielona") barrel, respectively. We assume the number of barrels l is not greater than 2000, and that there are at least 3 green barrels on the chute.

The schedule of ordering the barrels by the crane can be written in a form of a finite sequence R = (r1, ..., rk) of positive integers not greater than l - 2. The i-th element ri of the sequence R is the position (counting from the bottom) of the lowest of the three barrels that are to be lifted and moved up to the end in the i-th move.


Suppose we have 9 barrels arranged on the chute in the order: (red, green, blue, blue, red, blue, green, green, blue). They are represented by the sequence (c, z, n, n, c, n, z, z, n).

The crane working according to the schedule R = (6, 2, 5) will change their arrangement as follows: (c, z, n, n, c, n, n, z, z), (c, c, n, n, z, z, z, n, n), (c, c, n, n, n, n, z, z, z), so after three moves they will be ordered red-blue-green.


Write a program that:
  • reads from the text file POC.IN a number of barrels l and a sequence of l letters c, n, z determining the initial arrangement of the barrels on the chute,
  • composes a schedule of ordering the barrels red-blue-green; the schedule should be in the form of an appropriate finite sequence of positive integers and should be written in the text file POC.OUT.

For each initial arrangement of barrels (assuming that there are at least three green ones) there exist many schedules of ordering them red-blue-green. They may differ in the number of moves. Your program should find and write only one of them. The number of moves has not to be minimal for the solution to be correct. However, you should design your program to find schedules of ordering barrels in a possibly small number of moves and to find them as fast as possible.


  • In the first line of the text file POC.IN there is one integer l not less than 3 and not greater than 2000. It is the number of barrels on the chute.
  • In each of the following l lines there is one character written - a letter c, n, or z determining the colour of the successive barrel on the chute. There are at least 3 letters z in the sequence.


  • In the text file POC.OUT one should write an appropriate finite sequence of positive integers that is a schedule of ordering the barrels.
  • Each number of this sequence should be written in a separate line.


For the file POC.IN:
The following sequence in the file POC.OUT is a correct solution:

Your program should look for the file POC.IN in the current directory and create the file POC.OUT also in the current directory. The source file containing the program written by you should be named POC.???, where ??? are substituted by at most three-letter abbreviation of the programming language used. The same program in an executable form should be written in a file POC.EXE.

Print friendly version