Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void solve(){
- const int N = 2e5 + 10;
- int n;
- int a[N];
- cin >> n;
- for (int i = 1; i <= n; i++){
- cin >> a[i];
- }
- map <int, int> cnt;
- set <pair <int, int>> st;
- int ans = -1;
- for (int l = 1; l <= n; l++){
- st.erase({cnt[a[l]], a[l]});
- cnt[a[l]]++;
- st.insert({cnt[a[l]], a[l]});
- int mn = st.begin() -> first;
- int mx = st.rbegin() -> first;
- if (st.begin() -> second == st.rbegin() -> second){
- ans = l;
- }
- if (mn == mx && mn == 1){
- ans = l;
- }
- if (mn == mx && mn == 2 && l == 2){
- ans = l;
- }
- int cntMN = 1, cntMX = 1;
- if (st.size() > 1){
- auto it = st.begin();
- it++;
- if (it -> first == mn){
- cntMN = 2;
- }
- it = st.end();
- it--;
- it--;
- if (it -> first == mx){
- cntMX = 2;
- }
- }
- if (mn + 1 == mx && cntMX == 1){
- ans = l;
- }
- if (mn == 1 && cntMN == 1){
- if (st.size() > 1){
- auto it = st.begin();
- it++;
- if (it -> first == mx){
- ans = l;
- }
- }
- }
- }
- cout << ans << endl;
- }
- int main(){
- ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); // Оптимизация
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement