#P14672. [ICPC 2025 Seoul R] CPEquivalence

    ID: 16496 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP2025动态规划优化ICPC笛卡尔树首尔

[ICPC 2025 Seoul R] CPEquivalence

题目描述

Given an integer array xx consisting of integers (namely, each item of xx is an integer), the Closest Position array (CP-array) CP(x)CP(x) is an array of length x|x| defined to be

$$CP(x)[i] = \max(\{j \mid j < i; x[j] \ge x[i]\} \cup \{-1\}) \text{ for all } 0 \le i < |x|,$$

where x[i]x[i] denotes the ii-th integer in xx, and the length x|x| is the number of integers in xx. In other words, CP(x)[i]CP(x)[i] is the greatest index of xx that is smaller than ii and whose item at that index is greater than or equal to x[i]x[i]. For example, when x=[64,2,5,100,100]x = [64, 2, 5, 100, 100], its CP-array is CP(x)=[1,0,0,1,3]CP(x) = [-1, 0, 0, -1, 3], and x=5|x| = 5.

We say that two integer arrays xx and yy are CP-equivalent if CP(x)=CP(y)CP(x) = CP(y). It is obvious that two CP-equivalent integer arrays xx and yy have the same length. For example, two arrays x=[64,2,5,100,100]x = [64, 2, 5, 100, 100] and y=[3,1,2,4,1]y = [3, 1, 2, 4, 1] are CP-equivalent because their CP-arrays are the same as [1,0,0,1,3][-1, 0, 0, -1, 3].

For an integer array xx, an integer aa and a non-negative integer i<xi < |x|, a substitution operation on xx at position ii into aa returns the array $[x[0], x[1], \cdots, x[i-1], a, x[i+1], \cdots, x[|x|-1]]$. For an integer array xx and a non-negative integer i<xi < |x|, a deletion operation on xx at position ii returns the array $[x[0], x[1], \cdots, x[i-1], x[i+1], \cdots, x[|x|-1]]$. Finally, for an integer array xx, an integer aa and a non-negative integer ixi \le |x|, an insertion operation on xx at position ii returns the array $[x[0], x[1], \cdots, x[i-1], a, x[i], \cdots, x[|x|-1]]$. An edit operation on xx is one of an insertion, a deletion or a substitution at a single position.

Given two integer arrays xx and yy, compute the minimum number of edit operations on yy to obtain an array yy' satisfying CP(x)=CP(y)CP(x) = CP(y').

For example, let x=[64,2,5,100,100]x = [64, 2, 5, 100, 100] and y=[5,5,5,5]y = [-5, -5, -5, -5]. Consider the array y=[5,6,5,4,5]y' = [-5, -6, -5, -4, -5]. Then, we have CP(y)=[1,0,0,1,3]CP(y') = [-1, 0, 0, -1, 3] and therefore xx and yy' are CP-equivalent. Then, we can obtain the integer array yy' applying two edit operations on yy and it is minimum.

输入格式

Your program is to read from standard input. The input consists of three lines. The first line consists of two integers nn and mm (1n401 \le n \le 40; 1m401 \le m \le 40) that indicate the length of xx and yy, respectively. The second line consists of nn integers between 1,000,000-1,000,000 and 1,000,0001,000,000 (both inclusive), representing the array xx. The third line consists of mm integers between 1,000,000-1,000,000 and 1,000,0001,000,000 (both inclusive), representing the array yy.

输出格式

Your program is to write to standard output. Print exactly one line containing the minimum number of edit operations on yy to obtain an integer array yy' satisfying CP(x)=CP(y)CP(x) = CP(y').

5 5
64 2 5 100 100
3 1 2 4 1
0
5 4
64 2 5 100 100
-5 -5 -5 -5
2
6 5
1 2 3 4 5 6
2 5 3 4 5
3
6 3
1 3 5 2 5 2
5 5 6
3