1163B2.md (2513B)
1 Source: https://codeforces.com/problemset/problem/1163/B2 2 3 B2. Cat Party (Hard Edition) 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 This problem is same as the previous one, but has larger constraints. 22 23 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. 24 25 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. 26 27 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. 28 29 Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. 30 31 Input 32 33 The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. 34 35 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. 36 37 Output 38 39 Print a single integer $x$ — the largest possible streak of days. 40 41 Examples 42 43 Input 44 45 ``` 46 13 47 1 1 1 2 2 2 3 3 3 4 4 4 5 48 ``` 49 50 Output 51 52 ``` 53 13 54 ``` 55 56 Input 57 58 ``` 59 5 60 10 100 20 200 1 61 ``` 62 63 Output 64 65 ``` 66 5 67 ``` 68 69 Input 70 71 ``` 72 1 73 100000 74 ``` 75 76 Output 77 78 ``` 79 1 80 ``` 81 82 Input 83 84 ``` 85 7 86 3 2 1 1 4 5 1 87 ``` 88 89 Output 90 91 ``` 92 6 93 ``` 94 95 Input 96 97 ``` 98 6 99 1 1 1 2 2 2 100 ``` 101 102 Output 103 104 ``` 105 5 106 ``` 107 108 Note 109 110 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. 111 112 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.