Easily Broadcastable Tensors

시간 제한1초메모리 제한2048 MB

요약
두 차원 크기 수열이 주어질 때, 뒤에서부터 정렬한 각 쌍이 서로 같거나 둘 중 하나가 1이 되도록 1을 최소 개수만큼 끼워 넣는 문제이다.
난이도

보통10점 중 7점

유형
동적 계획법, 배열
정답자
아직 제출이 없습니다

문제

Little I is learning to use PyTorch. It is a very popular Python library for machine learning training.

Little I noticed that PyTorch has a mechanism for tensor operations called "broadcasting". You can think of tensors as multidimensional arrays. For a kk-dimensional tensor AA, we use a sequence of length kk denoted as (a_1,a_2,…,a_k)(a\_1, a\_2, \ldots, a\_k) to represent the lengths of its dimensions, meaning AA is a tensor of size a_1×a_2×…×a_ka\_1 \times a\_2 \times \ldots \times a\_k.

For two tensors AA and BB, with dimensions (a_1,a_2,…,a_m)(a\_1, a\_2, \ldots, a\_m) and (b_1,b_2,…,b_n)(b\_1, b\_2, \ldots, b\_n), respectively, AA and BB are easily broadcastable if and only if the following property holds:

For any integer 0≤i≤min⁡(n,m)−10 \le i \le \min(n, m) - 1, either a_m−i=b_n−ia\_{m - i} = b\_{n - i} or at least one of a_m−ia\_{m - i} and b_n−ib\_{n - i} is 11.

Now, Little I has two tensors with dimensions (p_1,p_2,…,p_m)(p\_1, p\_2, \ldots, p\_m) and (q_1,q_2,…,q_n)(q\_1, q\_2, \ldots, q\_n), and they may not be easily broadcastable.

To make them easily broadcastable, Little I can use several operations (or none), where each operation modifies the sequence pp or qq as follows:

Choose pp or qq, and insert a 11 at any position in the chosen sequence.

Little I wants to know the minimum number of operations required to make the two tensors easily broadcastable.

입력

The first line contains two integers mm and nn (1≤m,n≤20001 \le m, n \le 2000) representing the dimensions of the two tensors.

The second line contains mm integers p_1,p_2,…,p_mp\_1, p\_2, \ldots, p\_m (1≤p_i≤2000)1 \le p\_i \le 2000) describing the length of each dimension for the first tensor.

The third line contains nn integers q_1,q_2,…,q_nq\_1, q\_2, \ldots, q\_n (1≤q_i≤2000)1 \le q\_i \le 2000) describing the length of each dimension for the second tensor.

출력

Print a line with a single integer: the minimum number of insertions of 11 required to make the two tensors easily broadcastable.

힌트

In the example, inserting a 11 before the second position in sequence qq (resulting in 4 1 2) makes the two tensors easily broadcastable.

예제1

  1. 예제 1

    입력
    4 2
    2 1 3 2
    4 2
    
    예상 출력
    1