#P15705. [2018 KAIST RUN Spring] Zigzag

[2018 KAIST RUN Spring] Zigzag

题目描述

A sequence is called “Zigzag” if no three of its consecutive elements are monotone.

More formally, if sequence AA of length NN is Zigzag if, for all ii (1iN21 \leq i \leq N - 2), neither AiAi+1Ai+2A_i \leq A_{i+1} \leq A_{i+2} nor AiAi+1Ai+2A_i \geq A_{i+1} \geq A_{i+2} holds.

For given sequence AA of length NN, you should find a longest subsegment of AA which is a Zigzag sequence. Sequence BB of length MM is subsegment of sequence AA of length NN if, for some ii, B1=AiB_1 = A_i, B2=Ai+1B_2 = A_{i+1} \cdots, BM=Ai+M1B_M = A_{i+M-1} holds.

输入格式

Input consists of two lines.

The first line contains integer NN, length of sequence AA.

The second line contains space-separated NN integers. iith number is AiA_i.

输出格式

Print out the length of longest subsegment of AA which is a Zigzag sequence.

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

提示

Constraints

  • 3N5,0003 \leq N \leq 5,000
  • 1Ai1091 \leq A_i \leq 10^9 (1iN1 \leq i \leq N)