Codeforces B Cat Party (思维 推荐)

B2. Cat Party (Hard Edition)

time limit per test:1 second

memory limit per test:256 megabytes

 

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 ? 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 ?-th day has a ribbon with color ??ui. Shiro wants to know the largest number ?, such that if we consider the streak of the first ? days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining ?−1will have the same number of occurrences.

For example, consider the following sequence of ??: [2,2,1,1,5,4,4,5]. Then ?=7makes a streak, since if we remove the leftmost ??=5, each ribbon color will appear exactly twice in the prefix of ?−1days. Note that ?=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 ?n (1≤?≤10^5) — the total number of days.

The second line contains ?n integers ?1,?2,…,??(1≤??≤10^5) — the colors of the ribbons the cats wear.

Output

Print a single integer ? — the largest possible streak of days.

Examples

input

13
1 1 1 2 2 2 3 3 3 4 4 4 5

output

13

input

5
10 100 20 200 1

output

5

input

1
100000

output

1

input

7
3 2 1 1 4 5 1

output

6

input

6
1 1 1 2 2 2

output

5

Note

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 10days (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 66 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.

题目链接:https://codeforces.com/problemset/problem/1163/B2

题目大意:求一个最大数字x,使得u[1]~u[x]中在必须删除一个位置的条件下,其余数字出现的频数相同

题目分析:设u[i]出现的频数为a,频数a在1~i中出现的次数为b,题目要求1~i中必须删除一个位置,那么若a*b=i-1则说明当前的i可行,还有像样例一这样的情况,即a*b=i,若此时i+1<=n,则带上i+1也满足题目要求

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1e5 + 5;
int n, u, frq[MAX], frqCnt[MAX];

int main() {
    scanf("%d", &n);
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &u);
        frq[u]++;
        frqCnt[frq[u]]++;
        if (i != n && frq[u] * frqCnt[frq[u]] == i) {
            ans = i + 1;
        } else if (frq[u] * frqCnt[frq[u]] == i - 1) {
            ans = i;
        }
    }
    printf("%d\n", ans);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值