In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you are familiar with IRC chat, the support team is also reachable on PIRC network (irc.pirc.pl
) in #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.
This task is a harder version of task Tree Rotations from the second stage of 18th Polish OI. It wasn't used in the contest itself.
Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some interesting features:
Byteasar is from the old town of Byteburg and, like all true Byteburgers, praises neatness and order. He wonders how neat can his tree become thanks to appropriate rotations. The neatness of a tree is measured by the number of inversions in its corona, i.e. the number of pairs , such that in the corona .
The original tree (on the left) with corona has two inversions.
A single rotation gives a tree (on the right) with corona , which has only one inversion.
Each of these two trees has 5 branches.
Write a program that determines the minimum number of inversions in the corona of Byteasar's tree that can be obtained by rotations.
In the first line of the standard input there is a single integer () that denotes the number of leaves in Byteasar's tree. Next, the description of the tree follows. The tree is defined recursively:
In the first and only line of the standard output a single integer is to be printed: the minimum number of inversions in the corona of the input tree that can be obtained by a sequence of rotations.
For the input data:
3 0 0 3 1 2
the correct result is:
1
Explanation of the example: Figure 1 illustrates the tree given in the example.
Task authors: Tomasz Kociumaka, Tomasz Walen.