CF1163B2.Cat Party (Hard Edition)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 uiu_i . 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 x1x - 1 will have the same number of occurrences.

For example, consider the following sequence of uiu_i : [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=5u_i = 5 , each ribbon color will appear exactly twice in the prefix of x1x - 1 days. Note that x=8x = 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 nn ( 1n1051 \leq n \leq 10^5 ) — the total number of days.

The second line contains nn integers u1,u2,,unu_1, u_2, \ldots, u_n ( 1ui1051 \leq u_i \leq 10^5 ) — the colors of the ribbons the cats wear.

输出格式

Print a single integer xx — 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 1313 days, 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.

首页