## COCI '06 Contest 3 #6 Lista

View as PDF

Points: 20 (partial)
Time limit: 0.6s
Memory limit: 32M

Problem types

Mirko received a birthday present from his aunt in the US – a brand-new doubly-linked list (an example of which is shown in the figure below). The list contains nodes numbered through . Two types of moves can be done on the list:

• A) Move node in front of node .
• B) Move node after node .

An example of a list with nodes.

The list after the move A 1 4.

The list after another move, B 3 5.

Mirko played with his new toy for hours, writing down each move on a piece of paper so that he can reconstruct the list's initial state (nodes through in order from left to right).

When he decided to reconstruct the list, Mirko was astonished to find that there is no easy way to invert the moves and restore the list's initial state. Mirko cannot know where node was prior to each move, only where it ended up.

Seeing how Mirko is still recovering from the shock, write a program that finds a minimal sequence of moves that restored the list's initial state from Mirko's logs.

#### Input Specification

The first line of input contains two integers and , the number of nodes and the number of moves made by Mirko.

Each of the next rows contains a description of a single move made by Mirko – the type of move (A or B) and two integers and .

#### Output Specification

Output the minimum number of moves (call this number ) on the first line.

Each of the next lines should contain a description of a single move in the same format as in the input.

Note: The sequence need not be unique.

#### Scoring

If both the number and the sequence of moves are correct, your program will score full points on the test case.

If your program outputs the correct number and does not output the sequence of moves, or the sequence of moves is incorrect, you will get of the points for that test case.

#### Sample Input 1

2 1
A 2 1

#### Sample Output 1

1
A 1 2

#### Sample Input 2

4 3
B 1 2
A 4 3
B 1 4

#### Sample Output 2

2
A 1 2
B 4 3

#### Sample Input 3

6 5
A 1 4
B 2 5
B 4 2
B 6 3
A 3 5

#### Sample Output 3

3
A 4 5
B 6 5
A 2 3