UPC-7221: Flower(思维)

本文解析了一道关于修剪花朵使其高度相等的算法题,通过分析不同高度的花朵如何通过修剪达到统一高度,探讨了算法实现及优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值