Description:
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
题意:
给出n个高度,每次对n-1个数减去一,求最少几次可以使得高度一致,高度最低为1,如果不可以输出-1;
首先找到最高的那一个,因为每次操作都要对最高的操作。可以看做其他的和最高的相差多少。假设最高的要变成最低的,中间的那些肯定要多剪,之后再按照顺序把多剪的给加上就可以了。如果不懂可以看下面这个例子
5 8 9 10
5 7 8 9
5 6 7 8
5 5 6 7
5 4 5 6
5 3 4 5
4 3 3 4
3 3 2 3
2 2 2 2
AC代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
int a[100010];
int i,j,k;
int n,m;
ll ans;
ll cnt;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
cnt=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
for(i=0;i<n-1;i++)
{
cnt+=a[n-1]-a[i];
}
if(cnt>=a[n-1])
cout<<"-1"<<endl;
else
cout<<cnt<<endl;
}
return 0;
}