#P3541. [POI 2010] Monotonicity
[POI 2010] Monotonicity
Problem Description
Translated from POI 2010 Stage 3. Day 0 “Monotonicity”.
For an integer sequence , we define its “monotonic sequence” as a symbol sequence consisting of , , and , where the symbol represents the relation between and . For example, the monotonic sequence of is .
For an integer sequence and its monotonic sequence , if a symbol sequence satisfies that for all we have , then we say the sequence “implements” the sequence . In other words, the sequence can be obtained by repeating the sequence multiple times and then deleting a suffix. For example, the integer sequence implements at least the following symbol sequences:
Given an integer sequence and a monotonic sequence , find the longest subsequence such that the monotonic sequence of the subsequence implements the given symbol sequence.
Input Format
The first line contains two integers separated by spaces, representing the length of the integer sequence and the length of the monotonic sequence .
The second line contains integers separated by spaces, representing the sequence .
The third line contains symbols separated by spaces, representing the symbol sequence .
Output Format
The first line outputs an integer , representing the length of the longest subsequence of whose monotonic sequence “implements” the monotonic sequence .
The second line outputs any such subsequence , with elements separated by spaces.
7 3
2 4 3 1 3 5 3
< > =
6
2 4 3 3 5 3
Hint
For of the testdata, , , , .
Translated by ChatGPT 5