Flower

Rabbit loves flowers very much and she plants n pots of flowers in her house. But she never prunes them because she is lazy. So the flowers have different heights and look ugly. One day, Kayaking decides to prune the flowers to make them look beautiful. To make them have equal heights, smart Kayaking has a wonderful idea. Initially, the i-th pot of flower’s height is a[i]. Each time, Kayaking will select n-1 pots of flowers and prune them to make their height subtract 1 with his 49m knife. Exactly, the height of the flowers is at least 1. Now, Kayaking wants to know if it is possible to prune the flowers to make them have equal height. If possible, what is the minimum times he prunes the flowers. Could you tell him?
Input
The input starts with a line contains exactly one positive number T which is the number of test case.
Each test case starts with a line contains a number n indicates the number of flowers. Then there is a line contains n numbers indicates a[i].
Output
For each test case, print a single line containing one integer—the minimum times Kayaking prunes the flowers, or -1 if it is impossible.
Sample Input

2
5
1 2 2 2 2
5
1 2 2 2 3

Sample Output

1
-1

Hint
T<=10,n<=105,ai<=109

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <stdio.h>
typedef long long ll;
using namespace std;

int main()
{
    int T,n,flag;
    ll a[100001],i,sum;
    scanf("%d",&T);
    while(T--)
    {
    flag=1;
    sum=0;
       scanf("%d",&n);
       for(i=1;i<=n;i++)
       {
          scanf("%lld",&a[i]);
       }
       sort(a+1,a+n+1);
       for(i=2;i<=n;i++)
       {
          sum+=a[n]-a[i];
          if(sum>=a[1])
          {
            flag=0;break;
          }
       }
       sum+=a[n]-a[1];
       if(flag==0) printf("-1\n");
       else printf("%lld\n",sum);
    }
    return 0;
}
/*原理还不明白
5 8 9 10  ( 8 9 10 )砍 5刀
5 3 4 5   ( 5 4 5 ) 砍 1 刀
4 3 3 4   ( 4 3 3 ) 砍 1 刀
3 3 2 3
2 2 2 2 
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值