#P7686. [CEOI 2005] Multi-key Sorting
[CEOI 2005] Multi-key Sorting
Description
Consider a table with rows and columns. The columns are numbered from to . For simplicity, the entries in the table are strings consisting of lowercase letters.

You are given Sort() operations on such tables: Sort() sorts the rows of the table by the values in column (the order of columns does not change). The sorting is stable, meaning that rows with equal values in column keep their original relative order. For example, applying Sort() to table produces table .
We are interested in sequences of such sorting operations. These operations are applied one after another to the same table. For example, applying the sequence Sort(); Sort() to table produces table .
Two sequences of sorting operations are called equivalent if they have the same effect on any table. For example, Sort(); Sort(); Sort() is equivalent to Sort(); Sort(). However, it is not equivalent to Sort(); Sort(), because the effect on table is different.
Input Format
The first line contains two integers, and . is the number of columns, and is the number of sorting operations. The second line contains integers, . It defines the sequence of sorting operations Sort(); ; Sort().
Output Format
The first line contains an integer , the length of the shortest sorting-operation sequence that is equivalent to the given sequence. The second line contains integers, describing this shortest sequence.
4 6
1 2 1 2 3 3
3
1 2 3
Hint
Constraints
For of the data, , , .
Problem Notes
From Multi-key Sorting, CENTRAL-EUROPEAN OLYMPIAD IN INFORMATICS 2005.
Translated and organized by @求学的企鹅.
Translated by ChatGPT 5
京公网安备 11011102002149号