atcoder#ARC151E. [ARC151E] Keep Being Substring
[ARC151E] Keep Being Substring
Score : points
Problem Statement
You are given an integer sequence of length . Additionally, its contiguous subsequences of lengths and are given: and .
You can perform the four operations on below any number of times (possibly zero) in any order.
- Add an arbitrary integer at the beginning of .
- Delete the element at the beginning of .
- Add an arbitrary integer at the end of .
- Delete the element at the end of .
Here, must be a non-empty contiguous subsequence of before and after each operation. Find the minimum total number of operations needed to make equal . Under the Constraints of this problem, it is guaranteed that one can always make equal by repeating operations.
What is a contiguous subsequence?
A sequence is a contiguous subsequence of when there is an integer satisfying such that for every .
Constraints
- and are contiguous subsequences of .
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
7
3 1 4 1 5 7 2
2
3 1
3
1 5 7
3
You can make equal while keeping a non-empty contiguous subsequence of , as follows.
- First, delete the element at the beginning of . Now, you have .
- Next, add at the end of . Now, you have .
- Furthermore, add at the end of . Now, you have , which equal .
Here, you perform three operations, which is the fewest possible.
20
2 5 1 2 7 7 4 5 3 7 7 4 5 5 5 4 6 5 6 1
6
1 2 7 7 4 5
7
7 4 5 5 5 4 6
7