A12638 | Cat Party (Hard Edition)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $u_i$ . 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 $u_i$ : $[2, 2, 1, 1, 5, 4, 4, 5]$ . Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 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.
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 $u_i$ . 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 $u_i$ : $[2, 2, 1, 1, 5, 4, 4, 5]$ . Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 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.
输入格式
The first line contains a single integer $n$ ( $1 \leq n \leq 10^5$ ) — the total number of days.
The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ( $1 \leq u_i \leq 10^5$ ) — the colors of the ribbons the cats wear.
The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ( $1 \leq u_i \leq 10^5$ ) — the colors of the ribbons the cats wear.
输出格式
Print a single integer $x$ — the largest possible streak of days.
输入输出样例
输入 #1
13 1 1 1 2 2 2 3 3 3 4 4 4 5
输出 #1
13
输入 #2
5 10 100 20 200 1
输出 #2
5
输入 #3
1 100000
输出 #3
1
输入 #4
7 3 2 1 1 4 5 1
输出 #4
6
输入 #5
6 1 1 1 2 2 2
输出 #5
5
In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$ , $2$ , $3$ , and $4$ will have the same number of occurrences of $3$ . Note that the streak can also be $10$ days (by removing the $10$ -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 $6$ days, we can remove the third day from this streak then all of the remaining colors $1$ , $2$ , $3$ , $4$ and $5$ will occur exactly once.
In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$ , $2$ , $3$ , $4$ and $5$ will occur exactly once.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted