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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include <bits/stdc++.h> #define ll long long #define lll long long #define PII pair<int, int> namespace FAST_IO {
inline char nextChar() { static char buf[1000000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; } #define getch getchar template <class T> inline void read(T &x) { T flag = 1; x = 0; char ch = getch(); while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getch(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48), ch = getch(); } x *= flag; }
template <class T, class... _T> inline void read(T &x, _T &...y) { return read(x), read(y...); }
inline void print128(lll x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print128(x / 10); putchar(x % 10 + '0'); }
} using namespace std; using namespace FAST_IO; const ll mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-5; const int maxn = 1e3 + 10; const int maxm = 1e5 + 10; int t, n, m, k; class Solution { public: int maxEqualFreq(vector<int> &nums) { int ans = 1; unordered_map<int, int> mp1, mp2; int maxx = 0, minx = INF; for (int i = 0; i < nums.size(); i++) { if (mp1.count(nums[i])) { mp2[mp1[nums[i]]]--; } mp1[nums[i]]++; mp2[mp1[nums[i]]]++; maxx = max(maxx, mp1[nums[i]]); if (maxx == 1 || maxx * mp2[maxx] == i || (maxx - 1) * mp2[maxx - 1] + maxx == i + 1) { ans = i + 1; } } return ans; } }; int main() {
#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); cin >> n; vector<int> a; for (int i = 0, x; i < n; i++) { cin >> x; a.push_back(x); } Solution solution; cout << solution.maxEqualFreq(a) << endl; ; return 0; }
|