Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1225

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1225

Perfect Choir

 

Maratona de Programação da SBC Brazil

Timelimit: 1

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 effect. 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?

 

Input

 

The input contains several test cases. The first line of a test case contains an integer N (2 ≤ N ≤ 104) indicating the number of members of the choir. Notes are indicated by integers. The second line contains N integers, indicating the note (−105 ≤ notai ≤105) where 0 ≤ i N−1, that each singer must sing at the beginning of the song. Notes are given in non-decreasing order (notai notai+1).

 

Output

 

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.

 

 

 

Sample Input Sample Output

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

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>
using namespace std;
int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int n;
    while (cin >> n) {
        int resp = -1, total = 0;
        vector<int> v;
        for (int i = 0; i  <  n; i++) {
            int x;
            cin >> x;
            total += x;
            v.push_back(x);
        }
        if (total % n != 0) {
            cout << resp << endl;
            continue;
        }
        resp = 0;
        total /= n;
        for (int i = 0; i  <  n; i++) {
            resp += abs(v[i] - total);
        }
        cout << resp / 2 + 1 << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
1 2 3
4
3 6 9 12
6
1 2 3 4 5 723
5
10 10 10 10 10

Output

x
+
cmd
2
-1
601
1
Advertisements

Demonstration


Previous
#1218 Beecrowd Online Judge Solution 1218 Getline Three - Shoes Solution in C, C++, Java, Js and Python
Next
#1228 Beecrowd Online Judge Solution 1228 Start Grid Solution in C, C++, Java, Js and Python