Logo
(追記) (追記ここまで)

19345번 - Zigzag 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 256 MB136231417.722%

문제

We say that a sequence of numbers $x(1), x(2), \ldots, x(k)$ is zigzag if no three of its consecutive elements form a nonincreasing or nondecreasing sequence. More precisely, for all $i = 1, 2, \ldots, k-2,ドル either $x(i+1) < x(i)$ and $x(i+1) < x(i+2)$ or $x(i+1) > x(i)$ and $x(i+1) > x(i+2)$.

You are given two sequences of numbers $a(1), a(2), \ldots, a(n)$ and $b(1), b(2), \ldots, b(m)$. The problem is to compute the length of the longest common zigzag subsequence. In other words, you are going to remove elements from the two sequences so that they are equal, and what remains from each sequence is a zigzag sequence. If the minimum number of elements you have to remove is $k,ドル then your answer is $m+n-k$.

입력

The first line contains the length of the first sequence $n$ (1ドル\leq n \leq 2000$) followed by $n$ integers $a(1), a(2), \ldots, a(n)$. The second line contains the length of the second sequence $m$ (1ドル \leq m \leq 2000$) followed by $m$ integers $b(1), b(2), \ldots, b(m)$. All $a(i)$ and $b(i)$ are from the range $[-10^9, 10^9]$.

출력

Output one line containing the length of the longest common zigzag subsequence of $a$ and $b$.

제한

예제 입력 1

5 1 2 5 4 3
5 4 1 2 5 3

예제 출력 1

3

힌트

출처

Camp > Petrozavodsk Programming Camp > Winter 2018 > Day 9: Carnegie Mellon U Contest J번

  • 데이터를 추가한 사람: kyo20111
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /