#P17299. [ICPC 2026 Xi'an I] Split Sticks

    ID: 17190 Type: RemoteJudge 6000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 9 Uploaded By: Tags>ICPC2026省赛/邀请赛西安

[ICPC 2026 Xi'an I] Split Sticks

题目描述

Yuki has nn sticks arranged in a row, where the length of the ii-th stick is aia_i.

Yuki defines an operation as follows:

  • Choose a stick and cut it into two parts, both of which must have integer lengths (one part can have a length of 00).
  • Merge the left part of the cut stick with the stick immediately to its left; if there is no stick to its left, the left part becomes a new, independent stick.
  • Merge the right part of the cut stick with the stick immediately to its right; if there is no stick to its right, the right part becomes a new, independent stick.
  • Remove all sticks with a length of 00.

Now, Yuki wants to perform a number of operations such that all sticks have the same length. You need to help Yuki find the minimum number of operations required to make all sticks have the same length.

It can be proven that there always exists at least one sequence of operations that makes all sticks have the same length.

输入格式

This problem contains multiple test cases.

The first line contains a positive integer tt (1≤t≤105)(1 \le t \le 10^5), representing the number of test cases.

For each test case:

  • The first line contains a positive integer nn (1≤n≤106)(1 \le n \le 10^6).
  • The second line contains nn positive integers a1,…,ana_1, \dots, a_n (1≤ai≤106)(1 \le a_i \le 10^6).

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

输出格式

For each test case, output a single line containing an integer representing the minimum number of operations required to make all sticks have the same length.

3
3
1 5 4
4
1 4 2 5
5
3 3 3 3 3
1
2
0

提示

For the first test case:

  • In the first operation, choose the second stick and cut it into two parts of lengths 44 and 11. The lengths of the sticks from left to right become 5,55, 5, and all sticks have the same length.
  • It can be proven that the minimum number of operations required is 11.

For the second test case:

  • In the first operation, choose the first stick and cut it into two parts of lengths 00 and 11. The lengths of the sticks from left to right become 5,2,55, 2, 5.
  • In the second operation, choose the second stick and cut it into two parts of lengths 11 and 11. The lengths of the sticks from left to right become 6,66, 6, and all sticks have the same length.
  • It can be proven that the minimum number of operations required is 22.

For the third test case:

  • Initially, all sticks have the same length, so the minimum number of operations required is 00.