Description:
This problem is same as the previous one, but has larger constraints.
Shiro’s just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.
For each of the nn days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro’s house. The cat coming in the ii-th day has a ribbon with color uiui. Shiro wants to know the largest number xx, such that if we consider the streak of the first xx days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining x−1x−1 will have the same number of occurrences.
For example, consider the following sequence of uiui: [2,2,1,1,5,4,4,5][2,2,1,1,5,4,4,5]. Then x=7x=7 makes a streak, since if we remove the leftmost ui=5ui=5, each ribbon color will appear exactly twice in the prefix of x−1x−1 days. Note that x=8x=8doesn’t form a streak, since you must remove exactly one day.
Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.
Input:
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the total number of days.
The second line contains nn integers u1,u2,…,unu1,u2,…,un (1≤ui≤1051≤ui≤105) — the colors of the ribbons the cats wear.
Output:
Print a single integer xx — the largest possible streak of days.
Sample Input:
13 1 1 1 2 2 2 3 3 3 4 4 4 5
Sample Output:
13
Sample Input:
5 10 100 20 200 1
Sample Output:
5
Sample Input:
1 100000
Sample Output:
1
Sample Input:
7 3 2 1 1 4 5 1
Sample Output:
6
Sample Input:
6 1 1 1 2 2 2
Sample Output:
5
Note:
In the first example, we can choose the longest streak of 1313days, since upon removing the last day out of the streak, all of the remaining colors 11, 22, 33, and 44 will have the same number of occurrences of 33. Note that the streak can also be 1010 days (by removing the 1010-th day from this streak) but we are interested in the longest streak.
In the fourth example, if we take the streak of the first 66 days, we can remove the third day from this streak then all of the remaining colors 11, 22, 33, 44 and 55 will occur exactly once.
- 题意
- 确定最大的x,使得1~x的区间内删去一个数(指删去单个下标而非所有这个数)可以让其他所有的数的个数相同
- 题解
- 因为是最前的x个,所以删去的这个数可以是中间的,也可以是最后的
- 那么当我们读入一个数时,判断 该数出现的次数*与(该数出现的次数)相同的数的个数是否等于i(即可以删去后一个数)
- 或者 该数出现的次数*与(该数出现的次数)相同的数的个数是否等于i-1(即可以删去中间的某一个数)
- 代码
-
-
#include <bits/stdc++.h> using namespace std; long long n,x,ans,a[100010],b[100010]; int main() { cin>>n; ans=1; for(int i=1;i<=n;i++) { cin>>x; a[x]++; b[a[x]]++; if(a[x]*b[a[x]]==i&&i!=n) ans=i+1; if(a[x]*b[a[x]]==i-1) ans=i; } cout<<ans<<endl; return 0; }
-
-