Codeforces Round #558 (Div. 2)

 

B1. Cat Party (Easy Edition)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This problem is same as the next one, but has smaller 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 uiui. 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 x−1x−1 will have the same number of occurrences.

For example, consider the following sequence of uiui: [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=5ui=5, each ribbon color will appear exactly twice in the prefix of x−1x−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.

Input

The first line contains a single integer nn (1≤n≤1051≤n≤105) — the total number of days.

The second line contains nn integers u1,u2,…,unu1,u2,…,un (1≤ui≤101≤ui≤10) — the colors of the ribbons the cats wear.

Output

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

Examples

input

Copy

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

output

Copy

13

input

Copy

5
10 2 5 4 1

output

Copy

5

input

Copy

1
10

output

Copy

1

input

Copy

7
3 2 1 1 4 5 1

output

Copy

6

input

Copy

6
1 1 1 2 2 2

output

Copy

5

Note

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.

 

 

题意:选择一个i节点代表了,在从起点到i点的中,减少一个节点时,其余节点出现的次数相同

用一个二维数组装各个节点依次出现的次数,之后从后向前一个一个一维数组找起,判断出现的情况是否满足题意!

#include<bits/stdc++.h>
using namespace std;
const int maxn=100006;
int sum[16][maxn];
int a[16];

int judge(int x)
{
    int len=0;
    for(int i=1; i<=10; i++)
    {
        if( sum[i][x]!=0 )
        {
            len++;
            a[len]=sum[i][x];
        }
    }

    sort(a+1,a+1+len);

    if(len==1)
        return 1;

    if(a[1]==1 && a[2]==a[len] )
        return 1;

    if(a[1]==a[len-1] && a[1]==a[len]-1 )
        return 1;

    return 0;
}
int main()
{
    int n,m;
    int ans;
    scanf("%d",&n);
    ans=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&m);

        for(int j=1; j<=10; j++)
        {
            sum[j][i]=sum[j][i-1];
        }

        sum[m][i]++;
    }

    for(int i=n; i; i--)
        if( judge(i) )
        {
            ans=i;
            break;
        }
    cout<<ans<<endl;
}

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=100006;
int sum[16][maxn];
int a[16];

int judge(int x)
{
    int len=0;
    for(int i=1; i<=10; i++)
    {
        if( sum[i][x]!=0 )
        {
            len++;
            a[len]=sum[i][x];
        }
    }

    sort(a+1,a+1+len);

    if(len==1)
        return 1;

    if(a[1]==1 && a[2]==a[len] )
        return 1;

    if(a[1]==a[len-1] && a[1]==a[len]-1 )
        return 1;

    return 0;
}
int main()
{
    int n,m;
    int ans;
    scanf("%d",&n);
    ans=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&m);

        for(int j=1; j<=10; j++)
        {
            sum[j][i]=sum[j][i-1];
        }

        sum[m][i]++;
    }

    for(int i=n; i; i--)
        if( judge(i) )
        {
            ans=i;
            break;
        }
    cout<<ans<<endl;
}

 

A. Eating Soup

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The three friends, Kuro, Shiro, and Katie, met up again! It's time for a party...

What the cats do when they unite? Right, they have a party. Since they wanted to have as much fun as possible, they invited all their friends. Now nn cats are at the party, sitting in a circle and eating soup. The rules are simple: anyone having finished their soup leaves the circle.

Katie suddenly notices that whenever a cat leaves, the place where she was sitting becomes an empty space, which means the circle is divided into smaller continuous groups of cats sitting next to each other. At the moment Katie observes, there are mm cats who left the circle. This raises a question for Katie: what is the maximum possible number of groups the circle is divided into at the moment?

Could you help her with this curiosity?

You can see the examples and their descriptions with pictures in the "Note" section.

Input

The only line contains two integers nn and mm (2≤n≤10002≤n≤1000, 0≤m≤n0≤m≤n) — the initial number of cats at the party and the number of cats who left the circle at the moment Katie observes, respectively.

Output

Print a single integer — the maximum number of groups of cats at the moment Katie observes.

Examples

input

Copy

7 4

output

Copy

3

input

Copy

6 2

output

Copy

2

input

Copy

3 0

output

Copy

1

input

Copy

2 2

output

Copy

0

Note

In the first example, originally there are 77 cats sitting as shown below, creating a single group:

At the observed moment, 44 cats have left the table. Suppose the cats 22, 33, 55 and 77 have left, then there are 33 groups remaining. It is possible to show that it is the maximum possible number of groups remaining.

In the second example, there are 66 cats sitting as shown below:

At the observed moment, 22 cats have left the table. Suppose the cats numbered 33 and 66 left, then there will be 22 groups remaining ({1,2}{1,2} and {4,5}{4,5}). It is impossible to have more than 22 groups of cats remaining.

In the third example, no cats have left, so there is 11 group consisting of all cats.

In the fourth example, all cats have left the circle, so there are 00 groups.

餐桌问题:就是从一个餐桌围起来的圈子里,删除一些人,使得剩下的人尽可能的孤立,就是隔一个人选一个人删除;

规律题:if(m==0)
            ans=1;
        else
        {
            ans=min(n-m,m);
        }

 

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m;
int vis[1006];
int i,j;
int ans;
int flag;
int main()
{

    while(cin>>n>>m)
    {
        ans=0;
        if(m==0)
            ans=1;
        else
        {
            ans=min(n-m,m);
        }

        cout<<ans<<endl;

    }
}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值