#117. 消消乐(Array Shrinking)

消消乐(Array Shrinking)

题面翻译

题目描述

给你一个长度为 n(1n500)n(1 \le n \le 500) 的数组 aa,每次你可以进行以下两步操作:

  1. 找到 i[1,n)i \in [1, n),使得 ai=ai+1a_i = a_{i + 1}

  2. 它们 替换为 ai+1a_i + 1

每轮操作之后,显然数组的长度会减小 11,问剩余数组长度的最小值。

输入格式

第一行包含一个整数 n(1n500)n(1 \le n \le 500),表示数组的原长。

第二行包含 nn 个整数 a1,a2,an(1ai1000)a_1, a_2, \cdots a_n (1 \le a_i \le 1000),表示原数组 aa

输出格式

共一行仅一个整数 numnum,表示进行许多轮操作之后,aa 剩余长度的最小值。

样例 #1

样例输入 #1

5
4 3 2 2 3

样例输出 #1

2

样例 #2

样例输入 #2

7
3 3 4 4 4 3 3

样例输出 #2

2

样例 #3

样例输入 #3

3
1 3 5

样例输出 #3

3

样例 #4

样例输入 #4

1
1000

样例输出 #4

1

样例解释

第一组样例中,最优操作之一为 $\text{4 3 2 2 3} \to \text{4 3 3 3} \to \text{4 4 3} \to \text{5 3}$

第二组样例中,最优操作之一为 $\text{3 3 4 4 4 3 3} \to \text{4 4 4 4 3 3} \to \text{4 4 4 4 4} \to \text{5 4 4 4} \to \text{5 5 4} \to \text{6 4}$

对于第三和第四组样例,你并不能进行任何操作。

提示

In the first test, this is one of the optimal sequences of operations: 4 4 3 3 2 2 2 2 3 3 \rightarrow 4 4 3 3 3 3 3 3 \rightarrow 4 4 4 4 3 3 \rightarrow 5 5 3 3 .

In the second test, this is one of the optimal sequences of operations: 3 3 3 3 4 4 4 4 4 4 3 3 3 3 \rightarrow 4 4 4 4 4 4 4 4 3 3 3 3 \rightarrow 4 4 4 4 4 4 4 4 4 4 \rightarrow 5 5 4 4 4 4 4 4 \rightarrow 5 5 5 5 4 4 \rightarrow 6 6 4 4 .

In the third and fourth tests, you can't perform the operation at all.