CodeForces Global Round 2 A Ilya and a Colorful Walk

114 篇文章 0 订阅

http://codeforces.com/contest/1119/problem/A

Ilya lives in a beautiful city of Chordalsk.

There are nn houses on the street Ilya lives, they are numerated from 11 to nn from left to right; the distance between every two neighboring houses is equal to 11 unit. The neighboring houses are 11 and 22 , 22 and 33 , ..., n−1n−1 and nn . The houses nn and 11 are not neighboring.

The houses are colored in colors c1,c2,…,cnc1,c2,…,cn so that the ii -th house is colored in the color cici . Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.

Ilya wants to select two houses ii and jj so that 1≤i<j≤n1≤i<j≤n , and they have different colors: ci≠cjci≠cj . He will then walk from the house ii to the house jj the distance of (j−i)(j−i) units.

Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.

Help Ilya, find this maximum possible distance.

Input

The first line contains a single integer nn (3≤n≤3000003≤n≤300000 ) — the number of cities on the street.

The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤n1≤ci≤n ) — the colors of the houses.

It is guaranteed that there is at least one pair of indices ii and jj so that 1≤i<j≤n1≤i<j≤n and ci≠cjci≠cj .

Output

Print a single integer — the maximum possible distance Ilya can walk.

Examples

Input

5
1 2 3 2 3

Output

4

Input

3
1 2 1

Output

1

Input

7
1 1 3 1 1 1 1

Output

4

Note

In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5−1=45−1=4 units.

In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 11 unit.

In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7−3=47−3=4 units.

题目大意: 若i<j且ai!=aj,那么小明可以从i跳到j,(跳了j-i步)求小明可能跳的最大步数。

思路: 贪心,因为寻找的是i<j且ai !=aj 的两个数,因此i越靠前贡献越大,进一步思考,只需要记录前两个颜色首次出现的位置,后续操作更新最大值MAX即可,因为前两个颜色必定是最早出现的,那么它们的贡献值一定是最大的,所以这么贪心是正确的。具体维护看代码吧。

#include<iostream>
#include<cstdio>
#define INF 0x3f3f3f3f
using namespace std;

int c1,c2;
int p1,p2;

int main()
{
	int n;
	scanf("%d",&n);
	int t;
	int MAX=0;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&t);
		if(c1==0)//第一种颜色
		{
			c1=t;
			p1=i;
		}
		else if(c2==0&&t!=c1)//第二种颜色
		{
			c2=t;
			p2=i;
			MAX=max(MAX,p2-p1);//更新MAX
		}
		else if(t==c1&&p2!=0)//再次出现第一种颜色
			MAX=max(MAX,i-p2);
		else if(t==c2) //再次出现第二种颜色
			MAX=max(MAX,i-p1);
		else if(t!=c1&&t!=c2)//出现新的颜色 此时用第一种颜色更新最好
			MAX=max(MAX,i-p1);
	}
	printf("%d\n",MAX);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值