UPC-7221: Flower(思维)

7221: Flower

时间限制: 1 Sec  内存限制: 128 MB
提交: 137  解决: 35
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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?

输入

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].

输出

For each test case, print a single line containing one integer—the minimum times Kayaking prunes the flowers, or -1 if it is impossible.

样例输入

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

样例输出

1
-1

提示

T≤10,n≤10^5,ai≤10^9

来源/分类

2018黑龙江省赛 

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn= 100005;
int a[maxn];
int b[maxn];
int main(void)
{
        int T;cin>>T;
        while(T--)
        {
            int n;
            cin>>n;   
            for(int i=0;i<n;i++) cin>>b[i];
            //n = unique(b,b+n) - b;// 去重优秀操作 
            sort(b,b+n); 
            int sum=0;
            int flag=1;
            for(int i=1;i<n-1;i++)  
            {
                sum+=(b[n-1]-b[i]);   
                if(sum>=b[0]) {flag=0;break;}  
            }
            sum+=b[n-1]-b[0];
            if(!flag) cout<<"-1"<<endl;
            else cout<<sum<<endl;
        }
}

猜你喜欢

转载自blog.csdn.net/Achanss/article/details/82388174