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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章