AmazonOA #298
Minimum Insertions to Make List a Subsequence (Amazon Germany :)
估计困难可在线判题
initialList may contain duplicate integers, and the integers in finalList are distinct. In each step you may insert one positive integer at any position in initialList; you cannot delete or reorder existing elements. Find the minimum number of insertions so that finalList becomes a subsequence of the final initialList. Zeros and negative integers in the target cannot be obtained by insertion and must match existing elements; if it is impossible, output −1 as this site's protocol specifies.
输入格式
The first line contains m n, the second line contains the m integers of initialList, and the third line contains the n integers of finalList. On this site 1≤m,n≤200000, −10^9≤every element≤10^9, and finalList is distinct. The rule that only positive numbers may be inserted is not misread as requiring the original array to be all positive. The original statement does not define the output when there is no solution; this site does not exclude unsolvable inputs and explicitly encodes them as −1.
输出格式
Output a single integer, the answer.
样例 1
输入:
4 4
1 2 3 -1
-1 1 2 3
输出:
3
样例 2
输入:
2 3
1 3
1 2 3
输出:
1
样例 3
输入:
2 2
-1 0
0 -1
输出:
-1
样例说明
The answers are 3, 1 and −1. In the first example the final −1 must be kept, and then 1, 2 and 3 are inserted; in the third example the order of the anchors is impossible. One place in the original statement reverses the subsequence direction; based on Returns and the complete worked example, this site specifies that final is a subsequence of initial, and openly adds −1 for the no-solution case.