CF1462D.Add to Neighbour and Remove

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Polycarp was given an array of a[1…n]a[1 \dots n] of nn integers. He can perform the following operation with the array aa no more than nn times:

  • Polycarp selects the index ii and adds the value aia_i to one of his choice of its neighbors. More formally, Polycarp adds the value of aia_i to ai−1a_{i-1} or to ai+1a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
  • After adding it, Polycarp removes the ii-th element from the aa array. During this step the length of aa is decreased by 11.

The two items above together denote one single operation.

For example, if Polycarp has an array a=[3,1,6,6,2]a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:

  • Polycarp selects i=2i = 2 and adds the value aia_i to (i−1)(i-1)-th element: a=[4,6,6,2]a = [4, 6, 6, 2].
  • Polycarp selects i=1i = 1 and adds the value aia_i to (i+1)(i+1)-th element: a=[10,6,2]a = [10, 6, 2].
  • Polycarp selects i=3i = 3 and adds the value aia_i to (i−1)(i-1)-th element: a=[10,8]a = [10, 8].
  • Polycarp selects i=2i = 2 and adds the value aia_i to (i−1)(i-1)-th element: a=[18]a = [18].

Note that Polycarp could stop performing operations at any time.

Polycarp wondered how many minimum operations he would need to perform to make all the elements of aa equal (i.e., he wants all aia_i are equal to each other).

波利卡普得到了一个包含 nn 个整数的数组 a[1…n]a[1 \dots n]。他最多可对数组 aa 执行 nn 次如下操作:

  • 波利卡普选择一个下标 ii,并将 aia_i 的值加到其某个相邻元素上。更准确地说,波利卡普将 aia_i 的值加到 ai−1a_{i-1} 或 ai+1a_{i+1} 上(若该相邻位置不存在,则无法执行此加法)。
  • 加法完成后,波利卡普从数组 aa 中删除第 ii 个元素。此步骤使数组 aa 的长度减少 11。

以上两个步骤共同构成一次完整操作。

例如,若波利卡普拥有数组 a=[3,1,6,6,2]a = [3, 1, 6, 6, 2],则可对其执行如下操作序列:

  • 波利卡普选择 i=2i = 2,并将 aia_i 的值加到第 (i−1)(i-1) 个元素上:a=[4,6,6,2]a = [4, 6, 6, 2]。
  • 波利卡普选择 i=1i = 1,并将 aia_i 的值加到第 (i+1)(i+1) 个元素上:a=[10,6,2]a = [10, 6, 2]。
  • 波利卡普选择 i=3i = 3,并将 aia_i 的值加到第 (i−1)(i-1) 个元素上:a=[10,8]a = [10, 8]。
  • 波利卡普选择 i=2i = 2,并将 aia_i 的值加到第 (i−1)(i-1) 个元素上:a=[18]a = [18]。

注意:波利卡普可在任意时刻停止执行操作。

波利卡普想知道:为使数组 aa 中所有元素相等(即所有 aia_i 彼此相等),他至少需要执行多少次操作?

输入格式

The first line contains a single integer tt (1≤t≤30001 \leq t \leq 3000) — the number of test cases in the test. Then tt test cases follow.

The first line of each test case contains a single integer nn (1≤n≤30001 \leq n \leq 3000) — the length of the array. The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1051 \leq a_i \leq 10^5) — array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 30003000.

第一行包含一个整数 tt(1≤t≤30001 \leq t \leq 3000),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤30001 \leq n \leq 3000),表示数组的长度。下一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1051 \leq a_i \leq 10^5),即数组 aa。

保证所有测试用例中 nn 的总和不超过 30003000。

输出格式

For each test case, output a single number — the minimum number of operations that Polycarp needs to perform so that all elements of the aa array are the same (equal).

对于每个测试用例,输出一个整数——Polycarp 需要执行的最少操作次数,使得数组 aa 的所有元素都相同(相等)。

输入输出样例

  • 输入#1

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

    输出#1

    4
    2
    0
    2

说明/提示

In the first test case of the example, the answer can be constructed like this (just one way among many other ways):

[3,1,6,6,2][3, 1, 6, 6, 2] →i=4, add to left\xrightarrow[]{i=4,~add~to~left} [3,1,12,2][3, 1, 12, 2] →i=2, add to right\xrightarrow[]{i=2,~add~to~right} [3,13,2][3, 13, 2] →i=1, add to right\xrightarrow[]{i=1,~add~to~right} [16,2][16, 2] →i=2, add to left\xrightarrow[]{i=2,~add~to~left} [18][18]. All elements of the array [18][18] are the same.

In the second test case of the example, the answer can be constructed like this (just one way among other ways):

[1,2,2,1][1, 2, 2, 1] →i=1, add to right\xrightarrow[]{i=1,~add~to~right} [3,2,1][3, 2, 1] →i=3, add to left\xrightarrow[]{i=3,~add~to~left} [3,3][3, 3]. All elements of the array [3,3][3, 3] are the same.

In the third test case of the example, Polycarp doesn't need to perform any operations since [2,2,2][2, 2, 2] contains equal (same) elements only.

In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):

[6,3,2,1][6, 3, 2, 1] →i=3, add to right\xrightarrow[]{i=3,~add~to~right} [6,3,3][6, 3, 3] →i=3, add to left\xrightarrow[]{i=3,~add~to~left} [6,6][6, 6]. All elements of the array [6,6][6, 6] are the same.

在示例的第一个测试用例中,答案可以如下构造(这只是众多可行方式中的一种):

[3,1,6,6,2][3, 1, 6, 6, 2] →i=4, add to left\xrightarrow[]{i=4,~add~to~left} [3,1,12,2][3, 1, 12, 2] →i=2, add to right\xrightarrow[]{i=2,~add~to~right} [3,13,2][3, 13, 2] →i=1, add to right\xrightarrow[]{i=1,~add~to~right} [16,2][16, 2] →i=2, add to left\xrightarrow[]{i=2,~add~to~left} [18][18]。数组 [18][18] 的所有元素均相同。

在示例的第二个测试用例中,答案可以如下构造(这只是众多可行方式中的一种):

[1,2,2,1][1, 2, 2, 1] →i=1, add to right\xrightarrow[]{i=1,~add~to~right} [3,2,1][3, 2, 1] →i=3, add to left\xrightarrow[]{i=3,~add~to~left} [3,3][3, 3]。数组 [3,3][3, 3] 的所有元素均相同。

在示例的第三个测试用例中,Polycarp 无需执行任何操作,因为 [2,2,2][2, 2, 2] 仅包含相等(相同)的元素。

在示例的第四个测试用例中,答案可以如下构造(这只是众多可行方式中的一种):

[6,3,2,1][6, 3, 2, 1] →i=3, add to right\xrightarrow[]{i=3,~add~to~right} [6,3,3][6, 3, 3] →i=3, add to left\xrightarrow[]{i=3,~add~to~left} [6,6][6, 6]。数组 [6,6][6, 6] 的所有元素均相同。

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

首页