Mergesort is a quick sorting algorithm invented by John Von Neumann in 1945. In the heart of the algorithm lies a procedure that combines two already sorted sequences into a new sorted sequence. In this task you need to write a program which does a similar thing.
Let
In the beginning, your program knows only the lengths of sequences
cmp X Y
is a command which compares and returns if is less than , if is greater than and if they are equal.reverse X Y
is a command which reverses a subsequence of sequence between the and element (inclusive). For example, if sequence is equal to then thereverse 2 4
command will alter it so it becomes .end
is a command that denotes the end of interaction and your program should call it when the sequence is sorted in ascending order.
The cost of command reverse X Y
is equal to the length of the reversed subsequence (in other
words, end
) and additionally, so
that the total cost of all reverse
commands is at most
Interaction
Before interacting, you must read two integers from the standard input,
Afterwards, your program can give commands by outputting using standard output. Each
command must be output in its own line and has to be in the exact form of one of the three
commands described in the task. Regarding commands cmp
and reverse
,
After command cmp
, your program must input one integer using the standard input - the
command result. After other commands, your program must not input anything. After outputting
the command end
, your program needs to flush the standard output and finish executing.
Please note that you need to flush stdout after each command, or interaction may halt. In C++, this can be done with fflush(stdout)
or cout << flush
(depending on whether you use printf
or cout
). In Java, this can be done with System.out.flush()
. In Python, you can use sys.stdout.flush()
.
Constraints
Subtask | Points | Constraints |
---|---|---|
1 | 30 | |
2 | 30 | |
3 | 40 | No additional constraints. |
Sample Interaction
>>>
denotes your output. Do not print this out.
2 3
>>> cmp 1 4
-1
>>> reverse 2 5
>>> cmp 2 3
0
>>> reverse 4 5
>>> reverse 2 5
>>> end
Sample Explanation
Output | Input | Sequence | Total Cost |
---|---|---|---|
cmp 2 3 | |||
reverse 2 5 | |||
cmp 2 3 | |||
reverse 4 5 | |||
reverse 2 5 | |||
end |
Comments