Advertisement
miglss

4

Mar 21st, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(){
  5. const int N = 2e5 + 10;
  6. int n;
  7. int a[N];
  8. cin >> n;
  9. for (int i = 1; i <= n; i++){
  10. cin >> a[i];
  11. }
  12. map <int, int> cnt;
  13. set <pair <int, int>> st;
  14. int ans = -1;
  15. for (int l = 1; l <= n; l++){
  16. st.erase({cnt[a[l]], a[l]});
  17. cnt[a[l]]++;
  18. st.insert({cnt[a[l]], a[l]});
  19.  
  20. int mn = st.begin() -> first;
  21. int mx = st.rbegin() -> first;
  22. if (st.begin() -> second == st.rbegin() -> second){
  23. ans = l;
  24. }
  25. if (mn == mx && mn == 1){
  26. ans = l;
  27. }
  28. if (mn == mx && mn == 2 && l == 2){
  29. ans = l;
  30. }
  31. int cntMN = 1, cntMX = 1;
  32. if (st.size() > 1){
  33. auto it = st.begin();
  34. it++;
  35. if (it -> first == mn){
  36. cntMN = 2;
  37. }
  38. it = st.end();
  39. it--;
  40. it--;
  41. if (it -> first == mx){
  42. cntMX = 2;
  43. }
  44. }
  45. if (mn + 1 == mx && cntMX == 1){
  46. ans = l;
  47. }
  48. if (mn == 1 && cntMN == 1){
  49. if (st.size() > 1){
  50. auto it = st.begin();
  51. it++;
  52. if (it -> first == mx){
  53. ans = l;
  54. }
  55. }
  56. }
  57. }
  58. cout << ans << endl;
  59. }
  60.  
  61.  
  62. int main(){
  63. ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); // Оптимизация
  64. solve();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement