Zigzag
Time limit2sMemory limit256 MB
Find the longest sequence that is a subsequence of both given arrays and whose consecutive triples strictly alternate up and down.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
A sequence of numbers is zigzag if no three of its consecutive elements form a nonincreasing or nondecreasing sequence. More precisely, for all , either and , or and .
You are given two sequences and . Remove elements from both sequences so that they become equal and the remaining sequence is zigzag. If the minimum number of removed elements is , the answer is . Find the length of the longest common zigzag subsequence.
Input
The first line contains (), followed by integers . The second line contains (), followed by integers . Every and is in the range .
Output
Print one line containing the length of the longest common zigzag subsequence of and .