CF1637C.Andrew and Stones

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Andrew has nn piles with stones. The ii-th pile contains aia_i stones. He wants to make his table clean so he decided to put every stone either to the 11-st or the nn-th pile.

Andrew can perform the following operation any number of times: choose 33 indices 1≤i<j<k≤n1 \le i \lt j \lt k \le n, such that the jj-th pile contains at least 22 stones, then he takes 22 stones from the pile jj and puts one stone into pile ii and one stone into pile kk.

Tell Andrew what is the minimum number of operations needed to move all the stones to piles 11 and nn, or determine if it's impossible.

安德鲁有 nn 堆石头。第 ii 堆包含 aia_i 颗石头。他希望将桌面清理干净,因此决定将所有石头全部移至第 11 堆或第 nn 堆。

安德鲁可以执行以下操作任意多次:选择满足 1≤i<j<k≤n1 \le i \lt j \lt k \le n 的三个下标,且要求第 jj 堆中至少有 22 颗石头;然后他从第 jj 堆取走 22 颗石头,并分别向第 ii 堆和第 kk 堆各放入 11 颗石头。

请告诉安德鲁:将所有石头全部移至第 11 堆和第 nn 堆所需的最少操作次数是多少?若不可能完成,请予以判定。

输入格式

The input contains several test cases. The first line contains one integer tt (1≤t≤10 0001 \leq t \leq 10\,000) — the number of test cases.

The first line for each test case contains one integer nn (3≤n≤1053 \leq n \leq 10^5) — the length of the array.

The second line contains a sequence of integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — the array elements.

It is guaranteed that the sum of the values nn over all test cases does not exceed 10510^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤10 0001 \leq t \leq 10\,000),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(3≤n≤1053 \leq n \leq 10^5),表示数组的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \leq a_i \leq 10^9),表示数组的元素。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case print the minimum number of operations needed to move stones to piles 11 and nn, or print −1-1 if it's impossible.

对于每个测试用例,输出将石子移动到第 11 堆和第 nn 堆所需的最少操作次数;如果无法实现,则输出 −1-1。

输入输出样例

  • 输入#1

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

    输出#1

    4
    -1
    1
    -1

说明/提示

In the first test case, it is optimal to do the following:

  1. Select (i,j,k)=(1,2,5)(i, j, k) = (1, 2, 5). The array becomes equal to [2,0,2,3,7][2, 0, 2, 3, 7].
  2. Select (i,j,k)=(1,3,4)(i, j, k) = (1, 3, 4). The array becomes equal to [3,0,0,4,7][3, 0, 0, 4, 7].
  3. Twice select (i,j,k)=(1,4,5)(i, j, k) = (1, 4, 5). The array becomes equal to [5,0,0,0,9][5, 0, 0, 0, 9]. This array satisfy the statement, because every stone is moved to piles 11 and 55.

There are 44 operations in total.

In the second test case, it's impossible to put all stones into piles with numbers 11 and 33:

  1. At the beginning there's only one possible operation with (i,j,k)=(1,2,3)(i, j, k) = (1, 2, 3). The array becomes equal to [2,1,2][2, 1, 2].
  2. Now there is no possible operation and the array doesn't satisfy the statement, so the answer is −1-1.

In the third test case, it's optimal to do the following:

  1. Select (i,j,k)=(1,2,3)(i, j, k) = (1, 2, 3). The array becomes equal to [2,0,2][2, 0, 2]. This array satisfies the statement, because every stone is moved to piles 11 and 33.

The is 11 operation in total.

In the fourth test case, it's impossible to do any operation, and the array doesn't satisfy the statement, so the answer is −1-1.

在第一个测试用例中,最优操作如下:

  1. 选择 (i,j,k)=(1,2,5)(i, j, k) = (1, 2, 5)。数组变为 [2,0,2,3,7][2, 0, 2, 3, 7]。
  2. 选择 (i,j,k)=(1,3,4)(i, j, k) = (1, 3, 4)。数组变为 [3,0,0,4,7][3, 0, 0, 4, 7]。
  3. 两次选择 (i,j,k)=(1,4,5)(i, j, k) = (1, 4, 5)。数组变为 [5,0,0,0,9][5, 0, 0, 0, 9]。该数组满足题意,因为所有石子均被移至第 11 堆和第 55 堆。

总共进行了 44 次操作。

在第二个测试用例中,无法将所有石子移入编号为 11 和 33 的堆中:

  1. 初始时唯一可行的操作是 (i,j,k)=(1,2,3)(i, j, k) = (1, 2, 3)。数组变为 [2,1,2][2, 1, 2]。
  2. 此时已无可行操作,且数组不满足题意,因此答案为 −1-1。

在第三个测试用例中,最优操作如下:

  1. 选择 (i,j,k)=(1,2,3)(i, j, k) = (1, 2, 3)。数组变为 [2,0,2][2, 0, 2]。该数组满足题意,因为所有石子均被移至第 11 堆和第 33 堆。

总共进行了 11 次操作。

在第四个测试用例中,无法执行任何操作,且数组不满足题意,因此答案为 −1-1。

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

首页