fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MOD = 1e9 + 7;
  6.  
  7. typedef long long ll;
  8. #define cerr cout
  9. #define sz(a) (int)((a).size())
  10. #define all(x) (x).begin(), (x).end()
  11. string to_string(string s) { return '"' + s + '"';}
  12. string to_string(char s) { return string(1, s);}
  13. string to_string(const char* s) { return to_string((string) s);}
  14. string to_string(bool b) { return (b ? "true" : "false");}
  15. template <typename A> string to_string(A);
  16. template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
  17. template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
  18. void debug_out() { cerr << endl; }
  19. template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
  20. #define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  21. inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
  22. inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
  23. inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
  24. inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
  25. inline int inv(int a){return binpow(a, MOD - 2);}
  26. int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1; int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}
  27.  
  28. const int N = 1e5 + 5;
  29. int a[N], cnt[N];
  30.  
  31. int main(){
  32. // ios_base::sync_with_stdio(0);
  33. // cin.tie(0); cout.tie(0);
  34.  
  35. int n; cin>>n;
  36. for(int i=0;i<n;++i){
  37. cin>>a[i]; cnt[a[i]]++;
  38. }
  39. set<pair<int, int> > s;
  40. for(int i=1;i<N;++i)if(cnt[i])s.insert({cnt[i], i});
  41.  
  42. for(int i=n-1;i>=0;--i){
  43. if(s.size() == 1){
  44. cout<<i + 1<<"\n"; return 0;
  45. }
  46. int val = a[i];
  47. auto it = s.begin();
  48. int first = (*it).first; it++;
  49. int secfirst = (*it).first;
  50.  
  51. auto it1 = s.rbegin(); int last = (*it1).first;
  52. if(i == 8)pr(s), pr(*it1);
  53. it1--;
  54. if(i == 8)pr(*it1);
  55. int seclast = (*it1).first;
  56. if((first == 1 && secfirst == last) || (last - 1 == first && seclast == first)){
  57. cout<<i + 1<<"\n"; return 0;
  58. }
  59. s.erase({cnt[val], val}); cnt[val]--;
  60. if(cnt[val] > 0)s.insert({cnt[val], val});
  61. }
  62. return 0;
  63. }
Success #stdin #stdout 0s 16024KB
stdin
241
1 2 2 4 9 7 5 7 10 9 3 7 4 6 3 1 6 6 4 3 4 8 10 3 8 10 4 10 10 5 2 8 3 2 5 8 10 3 6 7 4 6 4 8 8 8 2 3 8 9 3 10 10 6 5 2 8 3 9 2 5 5 10 8 9 4 8 7 10 8 7 4 2 8 1 9 6 5 8 2 4 4 8 9 3 3 10 8 9 2 8 8 8 6 7 4 10 2 10 1 8 4 10 9 8 4 1 3 2 2 1 7 8 8 6 9 6 9 2 7 6 2 9 1 6 4 8 6 5 7 1 9 7 4 4 3 7 2 5 2 1 9 6 6 3 5 8 9 7 9 2 6 9 4 3 3 5 7 3 7 5 3 4 4 6 9 10 8 4 9 4 7 1 10 8 5 9 10 7 6 2 9 2 7 9 3 3 1 5 5 7 5 4 6 5 9 5 2 1 9 10 10 8 3 2 6 2 10 8 4 7 7 7 10 1 10 4 6 5 5 7 3 7 7 5 4 10 4 3 7 7 4 10 1 9 7 9 3 2 8 6
stdout
[s]: {(1, 1), (1, 4), (1, 5), (1, 9), (1, 10), (2, 2), (2, 7)}
[*it1]: (2, 7)
[*it1]: (1, 10)
9