#P3541. [POI 2010] Monotonicity

[POI 2010] Monotonicity

Problem Description

Translated from POI 2010 Stage 3. Day 0 “Monotonicity”.

For an integer sequence a1,a2,,ana_1, a_2, \cdots, a_n, we define its “monotonic sequence” as a symbol sequence s1,s2,,sn1s_1, s_2, \cdots, s_{n-1} consisting of <<, >>, and ==, where the symbol sis_i represents the relation between aia_i and ai+1a_{i+1}. For example, the monotonic sequence of 2,4,3,3,5,32, 4, 3, 3, 5, 3 is <,>,=,<,><, >, =, <, >.

For an integer sequence b1,b2,,bn+1b_1, b_2, \cdots, b_{n+1} and its monotonic sequence s1,s2,,sns_1, s_2, \cdots, s_n, if a symbol sequence s1,s2,,sks_1', s_2', \cdots, s_k' satisfies that for all 1in1 \le i \le n we have si=s((i1)modn)+1s_i = s_{((i - 1) \bmod n) + 1}', then we say the sequence s1,s2,,sns_1, s_2, \cdots, s_n “implements” the sequence s1,s2,,sks_1', s_2', \cdots, s_k'. In other words, the sequence s1,s2,,sns_1, s_2, \cdots, s_n can be obtained by repeating the sequence s1,s2,,sks_1', s_2', \cdots, s_k' multiple times and then deleting a suffix. For example, the integer sequence 2,4,3,3,5,32, 4, 3, 3, 5, 3 implements at least the following symbol sequences:

  • <,>,=<, >, =
  • <,>,=,<,><, >, =, <, >
  • <,>,=,<,>,<,<,=<, >, =, <, >, <, <, =
  • <,>,=,<,>,=,>,><, >, =, <, >, =, >, >

Given an integer sequence a1,a2,,ana_1, a_2, \cdots, a_n and a monotonic sequence s1,s2,,sks_1, s_2, \cdots, s_k, find the longest subsequence ai1,ai2,,aima_{i_1}, a_{i_2}, \cdots, a_{i_m} (1i1<i2<<imn)(1 \le i_1 \lt i_2 \lt \cdots \lt i_m \le n) such that the monotonic sequence of the subsequence implements the given symbol sequence.

Input Format

The first line contains two integers n,kn, k separated by spaces, representing the length of the integer sequence aia_i and the length of the monotonic sequence sjs_j.

The second line contains nn integers separated by spaces, representing the sequence aia_i.

The third line contains kk symbols separated by spaces, representing the symbol sequence sjs_j.

Output Format

The first line outputs an integer mm, representing the length of the longest subsequence of a1,a2,,ana_1, a_2, \cdots, a_n whose monotonic sequence “implements” the monotonic sequence s1,s2,,sns_1, s_2, \cdots, s_n.

The second line outputs any such subsequence ai1,ai2,,aina_{i_1}, a_{i_2}, \cdots, a_{i_n}, with elements separated by spaces.

7 3
2 4 3 1 3 5 3
< > =
6
2 4 3 3 5 3

Hint

For 100%100\% of the testdata, 1n200001 \le n \le 20000, 1k1001 \le k \le 100, 1ai10000001 \le a_i \le 1000000, sj{<,>,=}s_j \in \{<, >, =\}.

Translated by ChatGPT 5