CF1462D.Add to Neighbour and Remove
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Polycarp was given an array of a[1…n] of n integers. He can perform the following operation with the array a no more than n times:
- Polycarp selects the index i and adds the value ai to one of his choice of its neighbors. More formally, Polycarp adds the value of ai to ai−1 or to ai+1 (if such a neighbor does not exist, then it is impossible to add to it).
- After adding it, Polycarp removes the i -th element from the a array. During this step the length of a is decreased by 1 .
The two items above together denote one single operation.
For example, if Polycarp has an array a=[3,1,6,6,2] , then it can perform the following sequence of operations with it:
- Polycarp selects i=2 and adds the value ai to (i−1) -th element: a=[4,6,6,2] .
- Polycarp selects i=1 and adds the value ai to (i+1) -th element: a=[10,6,2] .
- Polycarp selects i=3 and adds the value ai to (i−1) -th element: a=[10,8] .
- Polycarp selects i=2 and adds the value ai to (i−1) -th element: 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 a equal (i.e., he wants all ai are equal to each other).
输入格式
The first line contains a single integer t ( 1≤t≤3000 ) — the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n ( 1≤n≤3000 ) — the length of the array. The next line contains n integers a1,a2,…,an ( 1≤ai≤105 ) — array a .
It is guaranteed that the sum of n over all test cases does not exceed 3000 .
输出格式
For each test case, output a single number — the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
输入输出样例
输入#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] i=4, add to left [3,1,12,2] i=2, add to right [3,13,2] i=1, add to right [16,2] i=2, add to left [18] . All elements of the array [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] i=1, add to right [3,2,1] i=3, add to left [3,3] . All elements of the array [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] 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] i=3, add to right [6,3,3] i=3, add to left [6,6] . All elements of the array [6,6] are the same.