Codeforces contest 1119 problem A Ilya and a Colorful Walk (简单模拟)

Ilya lives in a beautiful city of Chordalsk.
There are
n
n
houses on the street Ilya lives, they are numerated from
1
1
to
n
n
from left to right; the distance between every two neighboring houses is equal to
1
1
unit. The neighboring houses are
1
1
and
2
2
,
2
2
and
3
3
, …,
n−1
n−1
and
n
n
. The houses
n
n
and
1
1
are not neighboring.
The houses are colored in colors
c
1
,
c
2
,…,
c
n
c1,c2,…,cn
so that the
i
i
-th house is colored in the color
c
i
ci
. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses
i
i
and
j
j
so that
1≤i<j≤n
1≤i<j≤n
, and they have different colors:
c
i

c
j
ci≠cj
. He will then walk from the house
i
i
to the house
j
j
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
n
n
(
3≤n≤300000
3≤n≤300000
) — the number of cities on the street.
The second line contains
n
n
integers
c
1
,
c
2
,…,
c
n
c1,c2,…,cn
(
1≤
c
i
≤n
1≤ci≤n
) — the colors of the houses.
It is guaranteed that there is at least one pair of indices
i
i
and
j
j
so that
1≤i<j≤n
1≤i<j≤n
and
c
i

c
j
ci≠cj
.
Output
Print a single integer — the maximum possible distance Ilya can walk.
Examples
Input
Copy
5
1 2 3 2 3
Output
Copy
4
Input
Copy
3
1 2 1
Output
Copy
1
Input
Copy
7
1 1 3 1 1 1 1
Output
Copy
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=4
5−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
1
1
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=4
7−3=4
units.
问题链接: http://codeforces.com/contest/1119/problem/A
问题简述: 给定n个房子,每个房子有一种颜色,找到两个不同颜色的房子的最大距离
问题分析: 这题我比较佛,想得复杂了,用两次循环来找,如果要找的房子之间的距离大于ans,则直接break,这样的最大可能的时间复杂度是O((n/2)2),也就是15w×15w,比较难卡过。结果最后发现只要从开始向最后找一次,再从最后向开始找一次,更新ans最大值就行了…
AC通过的C++语言程序如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#define ll long long
#define endl '\n'
#include<ctime>
using namespace std;

int main()
{
    //ios::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    int ans=0;
    int a[300005];
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=n;i>=1;i--)
        if(a[i]!=a[1])
            ans=max(ans,i-1);
    for(int i=1;i<=n;i++)
        if(a[i]!=a[n])
            ans=max(ans,n-i);
    printf("%d",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44012745/article/details/89073962
今日推荐