0%

1224.最大相等频率

1224.最大相等频率

题目描述:

给定一个正整数数组 nums,找出最长的符合一下要求的前缀:

  • 从该前缀中恰好删除一个元素之后,剩下的每元素出现的次数都是相同的。(0次也算)

数据范围:
$ 1\le n \le 10^5, 1\le nums_i \le 10^5 $

题解:

首先对前缀进行研究,发现只有三种类型的前缀频次符合要求:

  • 1,1,1,1,1,1,1
  • 1,k,k,k,k,k,k
  • k,k,k,k,k,k,k+1

可以记录最大频率 f 出现的次数 cnt.

只有当 f == 1 || f * cnt[f] + 1 == len || (f - 1) * cnt[f-1] + f == len 时才符合要求。

代码:

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
// #pragma GCC optimize(2)
#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');
}

} // namespace FAST_IO
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:
// 1,1,1,1,1
// 1,k,k,k,k
// k,k,k,k,k + 1
// 需要记录该频次出现的次数。
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()
{
// #define COMP_DATA
#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;
}