In the event of technical difficulties with Szkopuł, please contact us via email at firstname.lastname@example.org.
If you are familiar with IRC chat, the support team is also reachable on PIRC network (
#szkopul channel. If you are not, just use email.
Please do not ask us things like "how to solve task XYZ?".
Please remember that the support team has to sleep sometimes or go to work in real life.
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 letters , , , where is the number of barrels. The letters , , represent a red ("czerwona" in Polish), blue ("niebieska") and green ("zielona") barrel, respectively. We assume the number of barrels is not greater than , and that there are at least green barrels on the chute.
The schedule of ordering the barrels by the crane can be written in a form of a finite sequence of positive integers not greater than . The -th element of the sequence 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 -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 .
The crane working according to the schedule will change their arrangement as follows: , , , so after three moves they will be ordered red-blue-green.
Write a program that:
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.
For the input data:
9 c z n n c n z z n
the correct result is:
6 2 5
Task author: Andrzej Walat.