#P11004. [蓝桥杯 2024 省 Python B] 魔法巡游

[蓝桥杯 2024 省 Python B] 魔法巡游

Background

The statement of this problem is highly ambiguous. Below is a comparison between the Luogu version and the Lanqiao Cup official website testdata:

  • Luogu: resonance happens as long as the digits 0,2,40, 2, 4 appear.
  • Lanqiao Cup official website: resonance requires that the two numbers share a pair among 0,2,40, 2, 4 in common.

Because the statement is ambiguous, the testdata of this problem is for reference only.

Problem Description

In the Lanqiao Kingdom, two wizards, Xiao Lan and Xiao Qiao, are responsible for maintaining the order of space and time. Each of them has NN rune stones. Every stone is carved with a numeric symbol between 11 and 10910^9. Xiao Lan's stones are denoted as s1,s2,,sNs_1, s_2, \cdots, s_N, and Xiao Qiao's are denoted as t1,t2,,tNt_1, t_2, \cdots, t_N.

Their task is to travel through nodes in space-time using these rune stones. Each travel follows this rule: after Xiao Lan uses rune stone sis_i to reach a new node, Xiao Qiao must choose a rune stone with a larger index (that is, some tjt_j with j>ij > i) to go to the next node. Similarly, after Xiao Qiao arrives, Xiao Lan must choose a rune stone sks_k with index k>jk > j to continue the tour.

To travel successfully, the numeric symbols on two consecutively used rune stones must resonate. This resonance happens only when the numeric symbols contain at least one special element: Spark (digit 00), Ripple (digit 22), or Windwhisper (digit 44). For example, in the symbol sequence 126,552,24,4126, 552, 24, 4, every pair of consecutive runes contains at least one resonance element, so it is a successful tour sequence. But 15,51,515, 51, 5 does not work, because their resonance elements do not include any of Spark, Ripple, or Windwhisper.

Xiao Lan always starts first, using his rune stones to begin the tour.

Your task is to compute the maximum length of a space-time tour sequence that these two wizards can perform. Such a sequence has the form si1,ti2,si3,ti4,s_{i_1}, t_{i_2}, s_{i_3}, t_{i_4}, \cdots, where the indices satisfy i1<i2<i3<i4<i_1 < i_2 < i_3 < i_4 < \cdots, and every adjacent pair of rune stones in the sequence contains at least one resonance element.

Input Format

The first line contains an integer NN, representing the number of rune stones each wizard has.

The second line contains NN integers s1,s2,,sNs_1, s_2, \cdots, s_N, separated by spaces, representing the numeric symbols carved on Xiao Lan's rune stones.

The third line contains NN integers t1,t2,,tNt_1, t_2, \cdots, t_N, separated by spaces, representing the numeric symbols carved on Xiao Qiao's rune stones.

Output Format

Output one line containing one integer, representing the maximum number of space-time tours Xiao Lan and Xiao Qiao can perform while following all rules.

5
126 393 581 42 44
204 990 240 46 52

4

Hint

For 30%30\% of the test cases, 1N1031 \le N \le 10^3, 1si,ti1051 \le s_i, t_i \le 10^5.

For all test cases, 1N1051 \le N \le 10^5, 1si,ti1091 \le s_i, t_i \le 10^9.

Sample Explanation

Here, digit 22 serves as a resonance element connecting s1s_1 and t3t_3, and s4s_4 and t5t_5. Digits 22 and 44 serve as resonance elements connecting t3t_3 and s4s_4.

Translated by ChatGPT 5