Perfect Choir(模拟)

题目描述
The Conductor of the choir is planning to take part in the famous Brazilian Choir Week, and therefore she wants the choir to rehearse a new song, described as follows:
• each member of the choir starts singing one note, and only changes the note when determined by the Conductor;
• at the end of each bar measure, the Conductor determines that exactly two singers change the note they are singing: one singer starts to sing the note immediately above the note she sang,and another singer starts to sing the note immediately below the note she sang;
• the song finishes at the end of the first bar measure in which all singers are singing the same note.
The Conductor already has several ideas of how to distribute the notes among choir members at the beginning of the song, in order to create the desired eff ect. However, she is worried about whether,
given a note distribution for the singers, it is possible to reach the end of the song in the way she wants (all singing the same note). And, if that is possible, she wants to know the minimum number of bar measures the song can have. Can you help her?

输入
The input contains several test cases. The first line of a test case contains an integer N indicating the number of members of the choir. Notes are indicated by integers. The second line contains N integers, indicating the note that each singer must sing at the beginning of the song. Notes are given in non-decreasing order.

输出
For each test case, print a line containing a single integer indicating the minimum number of bar measures the song can have. If it is not possible to have all members singing the same note, then print
the value ‘-1’.
Restrictions
• 2 ≤ N ≤ 104
• −105 ≤ notei ≤ 105 for 0 ≤ i ≤ N − 1
• notei ≤ notei+1 for 0 ≤ i ≤ N − 2

样例输入
3
1 2 3
4
3 6 9 12
6
1 2 3 4 5 723
5
10 10 10 10 10

样例输出
2
-1
601
1

思路:

理解了题目的意思就不难
这题是说给出n个数据,然后每次可以选择两个数据,修改其中一个变高,另一个变低,看看最后能把这些数据都修改成同一个值,最小可以是多少,如果不能都修改成同一个值,输出-1

不难发现这个最低的值就是平均数,判断能不能修改成同一个值也就是看平均数是不是整数

AC代码

#include<iostream>
#include<vector>
#include<cmath>
 
using namespace std;
 
typedef long long ll;
 
ll n,ans,num,average;
vector<ll> a;
int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n)
    {
        ans = 0;
        num = 0;
        for(int i = 0; i < n; i++)
        {
            int tmp;
            cin >> tmp;
            num += tmp;
            a.push_back(tmp);
        }
        if(num%n != 0)
        {
            cout << "-1" << endl;
        }
        else
        {
            average = num/n;
            for(int i = 0; i < a.size(); i++)
            {
                ans += abs(average - a[i]);
            }
            cout << ans/2 + 1 << endl;
        }
        a.clear();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值