아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Sequence Conversion 2

시간 제한2초메모리 제한1024 MB

요약
인접한 두 원소를 xor로 합쳐 지그재그 배열로 만들 때 필요한 최소 연산 횟수를 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 그리디
정답자
아직 제출이 없습니다

문제

You are given an array of non-negative integers a_1,a_2,…,a_Na\_1, a\_2, \dots, a\_N.

You can perform the following operation several times:

  • Choose an index ii. (1≤i<1 \leq i < length of the array) Then, remove a_i,a_i+1a\_i, a\_{i+1} and replace them with a_i⊕a_i+1a\_i \oplus a\_{i+1}. (The total length of the array decreases by 1)

Expression x⊕yx \oplus y means bitwise xor of two numbers xx and yy. In binary representation, if the ii-th digit of x and y is equal, then the ii-th digit of x⊕yx \oplus y is 00, and if not, it is 11.

The given operation exists in all modern programming languages. For example, in C++ and Java, it is represented as x ∧ yx\ ^{\wedge}\ y.

You want to convert the given array into a zig-zag array.

We say an array of mm integers, z_1,z_2,...,z_mz\_1, z\_2, ..., z\_{m}, is a zig-zag array if no three consecutive elements in the array are either monotonically increasing or monotonically decreasing.

In other words, if there are three elements z_i,z_i+1,z_i+2z\_i, z\_{i+1}, z\_{i+2} in the array such that z_i≤z_i+1≤z_i+2z\_i \leq z\_{i+1} \leq z\_{i+2} or z_i≥z_i+1≥z_i+2z\_i \geq z\_{i+1} \geq z\_{i+2}, the array is not zig-zag. Otherwise, it is zig-zag array.

Find the minimum number of operations needed to convert a_i\\{a\_i\\} into a zig-zag array.

입력

The first line contains an integer NN, where NN denotes the length of the sequence.

The second line contains NN space-separated non-negative integers a_1,a_2,…,a_Na\_1, a\_2, \dots, a\_N.

출력

Print the minimum number of operations needed to change the sequence a_i\\{a\_i\\} into a zig-zag array.

제한

  • 1≤N≤3,0001 \leq N \leq 3\\,000
  • 0≤a_i<2300 \leq a\_i < 2^{30} (1≤i≤N)(1 \le i \le N)

예제2

  1. 예제 1

    입력
    3
    1 2 3
    
    예상 출력
    1
    
  2. 예제 2

    입력
    3
    1 3 2
    
    예상 출력
    0