-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1954B.cpp
More file actions
72 lines (61 loc) · 1.73 KB
/
1954B.cpp
File metadata and controls
72 lines (61 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#define MAXN 2000000000
#define llu unsigned long long
#include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
#ifdef DEBUG
freopen("in.in", "r", stdin);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v(n, 0);
for (auto &vv : v)
cin >> vv;
// observation:
// 1. what make an array beautiful?
// --> they should have peak, and the base level with value x, which is
// the final beautiful array's value.
// --> e.g. ___y___k___
// 2. what does an non-beautiful array look like?
// --> it has something like
// --> e.g. __yk____
int ans = std::numeric_limits<int>::max();
int base = 0;
unordered_map<int, int> um;
for (auto &vv : v)
um[vv]++;
int count = 0;
for (auto &p : um) {
if (p.second > count) {
count = p.second;
base = p.first;
}
}
// Now we know the base value. Find all the peak.
vector<int> peak;
for (int i = 0; i < v.size(); ++i) {
if (v[i] != base)
peak.push_back(i);
}
if (peak.size() >= 1)
ans = min(ans, peak[0]);
for (int i = 1; i < peak.size(); ++i) {
ans = min(ans, peak[i] - peak[i - 1] - 1);
}
if (peak.size() >= 1)
ans = min(ans, (int) v.size() - peak[peak.size() - 1] - 1);
cout << (ans == std::numeric_limits<int>::max() ? -1 : ans) << endl;
}
return 0;
}