C. Andrew and Stones--Codeforces Global Round 19

Problem - 1637C - Codeforces

C. Andrew and Stones

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Andrew has nn piles with stones. The ii-th pile contains aiai 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≤i<j<k≤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.

Input

The input contains several test cases. The first line contains one integer tt (1≤t≤100001≤t≤10000) — the number of test cases.

The first line for each test case contains one integer nn (3≤n≤1053≤n≤105) — the length of the array.

The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the array elements.

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

Output

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.

Example

input

Copy

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

output

Copy

4
-1
1
-1 

Note

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.

==================================================================================================================================================

当2<=i<=n-1时,若全是1,显然连操作都不能操作,一旦有一个大于等于2的,如果这个数字是偶数,我们可以把它消去,并且把其余奇数也变成偶数,由于左右两端是可以周转的,故可以达到灵活操作的目的。如果一个偶数也没有,也是可以通过先把一个奇数-2,一个奇数加1来实现的,这样最终的答案,跟我们对一个偶数减2,奇数加1得到的效果相同,由于无论-2还是+1,最终都难免把奇数多出来的1减去,故奇数的次数是奇数/2+1

# include<iostream>

typedef long long int ll;
using namespace std;
int a[100000+10];
int main ()
{

    int t;

    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int flag=0;

        ll ans=0;

        for(int i=1;i<=n;i++)
        {
            cin>>a[i];

            if(i>=2&&i<=n-1&&a[i]!=1)
                {
                    flag=1;
                }

                if(i>=2&&i<=n-1)
                {
                    if(a[i]%2==0)
                        ans+=a[i]/2;
                    else
                        ans+=a[i]/2+1;

                }

        }
        if(!flag)
        {
            cout<<"-1"<<endl;

            continue;


        }
        else if(n==3&&a[2]%2==1)
        {
            cout<<-1<<endl;

        }
        else
        {

            cout<<ans<<endl;

        }

    }
    return 0;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这段代码中有几个问题: 1. while 循环的判断条件应该是 `stones.length > 1`,因为只有至少有两个石头时才需要进行破碎的操作。所以应该将 `while( stones.length==1 || stones.length==0 )` 修改为 `while( stones.length > 1 )`。 2. 在交换石头的位置时,应该使用临时变量来保存一个石头的值,然后再进行交换。所以应该将 `a=stones[j];` 修改为 `int temp = stones[i];`,将 `stones[i]=stones[j];` 修改为 `stones[i]=stones[j];`,将 `stones[j]=a;` 修改为 `stones[j]=temp;`。 3. 在判断最后剩下的石头重量时,应该使用索引 `stones.length-1` 和 `stones.length-2` 来获取最后两个石头的重量。所以应该将 `if(stones[stones.length-1]<=stones[stones.length])` 修改为 `if(stones[stones.length-1]<=stones[stones.length-2])`,将 `stones.length=stones.length-2;` 修改为 `stones.length -= 2;`。 4. 最后返回的应该是 `stones[0]`,而不是 `stones[stones.length]`。所以应该将 `return stones[stones.length];` 修改为 `return stones[0];`。 修正后的代码如下: ```java class Solution { public int lastStoneWeight(int[] stones) { while (stones.length > 1) { for (int i = 0; i < stones.length; i++) { for (int j = 1; j < stones.length; j++) { if (stones[i] > stones[j]) { int temp = stones[i]; stones[i] = stones[j]; stones[j] = temp; } } } if (stones[stones.length - 1] <= stones[stones.length - 2]) { stones[stones.length - 2] -= stones[stones.length - 1]; stones = Arrays.copyOf(stones, stones.length - 1); } else { stones = Arrays.copyOf(stones, stones.length - 2); } } return stones[0]; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值