本文共 1941 字,大约阅读时间需要 6 分钟。
题目来源:
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 n 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 i-th day has a ribbon with color ui. Shiro wants to know the largest number x, such that if we consider the streak of the first x days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining x−1 will have the same number of occurrences.
For example, consider the following sequence of ui: [2,2,1,1,5,4,4,5]. Then x=7 makes a streak, since if we remove the leftmost ui=5, each ribbon color will appear exactly twice in the prefix of x−1 days. Note that x=8 doesn’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 n (1≤n≤105) — the total number of days.
The second line contains n integers u1,u2,…,un (1≤ui≤105) — the colors of the ribbons the cats wear.
Output
Print a single integer x — the largest possible streak of days.
Sample Input
13
1 1 1 2 2 2 3 3 3 4 4 4 5Sample Output
13
AC代码1:
#includeusing namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'const int MAXN = 1e5+5;int arr[MAXN]; int main(){ SIS; int n,x,ans=1; map m1,m2; cin >> n; for(int i=0;i > x; m1[x]++; m2[m1[x]]++; if(m1[x]*m2[m1[x]]==i+1 && i!=n-1) ans=i+2; else if(m1[x]*m2[m1[x]]==i) ans=i+1; } cout << ans << endl; return 0;}
转载地址:http://xsyof.baihongyu.com/