#P3928. SAC E#1 - 一道简单题 Sequence2

    ID: 2868 远端评测题 2000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树树状数组离散化洛谷原创O2优化洛谷月赛

SAC E#1 - 一道简单题 Sequence2

Description

Xiao Qiang likes sequences. One day, on a whim, he wrote down three sequences, each of length nn.

Amiba also likes sequences, but he only likes one type, the wave sequence.

Amiba told Xiao Qiang about his preference. Xiao Qiang plans to find the longest wave sequence within these three sequences.

That is, if we denote the three sequences as an,1,an,2,an,3a_{n,1},a_{n,2},a_{n,3}, he must construct a sequence of ordered pairs (pi,qi)(p_i,q_i) such that for any i>1i>1:

  • pi>pi1p_i>p_{i-1}.
  • If qi=0q_i=0, then api,qiapi1,qi1a_{p_i,q_i} \ge a_{p_{i-1},q_{i-1}}.
  • If qi=1q_i=1, then api,qiapi1,qi1a_{p_i,q_i} \le a_{p_{i-1},q_{i-1}}.
  • If qi=2q_i=2, it only needs to keep the same direction within a segment (that is, for a contiguous segment with qi=2q_i=2, either all satisfy api,qiapi1,qi1a_{p_i,q_i} \ge a_{p_{i-1},q_{i-1}}, or all satisfy api,qiapi1,qi1a_{p_i,q_i} \le a_{p_{i-1},q_{i-1}}).

Xiao Qiang wants this sequence of ordered pairs to be as long as possible.

Hint: When qiqi1q_i \not = q_{i-1}, the monotonicity is determined by qiq_i, not by qi1q_{i-1}.

Clear version of the problem statement

Xiao Qiang gets a 3×n3 \times n array. In each column, choose one number (or choose none), subject to the following conditions:

  1. If you choose from the first row, it must be greater than or equal to the previous number.
  2. If you choose from the second row, it must be less than or equal to the previous number.
  3. If you choose from the third row, then for any contiguous segment of numbers chosen from the third row, the directions must be the same (either all are less than or equal to the previous number, or all are greater than or equal to the previous number).

Input Format

The input contains 44 lines.

The first line contains an integer nn, the length of the sequences.

The 22nd, 33rd, and 44th lines each contain nn integers, representing the three sequences, respectively.

Output Format

Output a single integer, the length of the longest wave sequence.

6
1 2 3 6 5 4
5 4 3 7 8 9
1 2 3 6 5 4

6

Hint

Constraints:

  • For 20%20\% of the testdata, n10n \le 10, m1000m \le 1000.
  • For 60%60\% of the testdata, n1000n \le 1000, m1000m \le 1000.
  • For 100%100\% of the testdata, n105n \le 10^5, m109m \le 10^9.

Here m=max{ai}m=\max\{|a_i|\}.

Sample explanation:

Take 1, 2, 3 from the third row (increasing), then take 6 from the first row (increasing), then take 5, 4 from the third row (decreasing), for a length of 6.

Translated by ChatGPT 5