CF1312E.Array Shrinking

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array a1,a2,…,ana_1, a_2, \dots, a_n. You can perform the following operation any number of times:

  • Choose a pair of two neighboring equal elements ai=ai+1a_i = a_{i + 1} (if there is at least one such pair).
  • Replace them by one element with value ai+1a_i + 1.

After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array aa you can get?

给你一个数组 a1,a2,…,ana_1, a_2, \dots, a_n。你可以执行以下操作任意多次:

  • 选择一对相邻且相等的元素 ai=ai+1a_i = a_{i + 1}(要求至少存在这样的一对);
  • 将它们替换为一个值为 ai+1a_i + 1 的元素。

每次执行该操作后,数组长度减少一(元素下标相应重新编号)。你能得到的数组 aa 的最小可能长度是多少?

输入格式

The first line contains the single integer nn (1≤n≤5001 \le n \le 500) — the initial length of the array aa.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤10001 \le a_i \le 1000) — the initial array aa.

第一行包含一个整数 nn(1≤n≤5001 \le n \le 500)—— 数组 aa 的初始长度。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤10001 \le a_i \le 1000)—— 数组 aa 的初始值。

输出格式

Print the only integer — the minimum possible length you can get after performing the operation described above any number of times.

输出唯一的整数——即在执行上述操作任意次数后所能得到的最小可能长度。

输入输出样例

  • 输入#1

    5
    4 3 2 2 3

    输出#1

    2
  • 输入#2

    7
    3 3 4 4 4 3 3

    输出#2

    2
  • 输入#3

    3
    1 3 5

    输出#3

    3
  • 输入#4

    1
    1000

    输出#4

    1

说明/提示

In the first test, this is one of the optimal sequences of operations: 44 33 22 22 33 →\rightarrow 44 33 33 33 →\rightarrow 44 44 33 →\rightarrow 55 33.

In the second test, this is one of the optimal sequences of operations: 33 33 44 44 44 33 33 →\rightarrow 44 44 44 44 33 33 →\rightarrow 44 44 44 44 44 →\rightarrow 55 44 44 44 →\rightarrow 55 55 44 →\rightarrow 66 44.

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

在第一个测试用例中,以下是一种最优的操作序列:44 33 22 22 33 →\rightarrow 44 33 33 33 →\rightarrow 44 44 33 →\rightarrow 55 33。

在第二个测试用例中,以下是一种最优的操作序列:33 33 44 44 44 33 33 →\rightarrow 44 44 44 44 33 33 →\rightarrow 44 44 44 44 44 →\rightarrow 55 44 44 44 →\rightarrow 55 55 44 →\rightarrow 66 44。

在第三和第四个测试用例中,你根本无法执行该操作。

输入解题思路,AI测评打分。不知道怎么写?

首页