For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.
Max is a young biologist. For nn days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer kk that if the shark in some day traveled the distance strictly less than kk, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to kk; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least kk.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of nn days we can find consecutive nonempty segments when the shark traveled the distance less than kk in each of the days: each such segment corresponds to one location. Max wants to choose such kk that the lengths of all such segments are equal.
Find such integer kk, that the number of locations is as large as possible. If there are several such kk, print the smallest one.
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of days.
The second line contains nn distinct positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the distance traveled in each of the day.
Print a single integer kk, such that
- the shark was in each location the same number of days,
- the number of locations is maximum possible satisfying the first condition,
- kk is smallest possible satisfying the first and second conditions.
8 1 2 7 3 4 8 5 6
7
6 25 1 2 3 14 36
2
In the first example the shark travels inside a location on days 11 and 22 (first location), then on 44-th and 55-th days (second location), then on 77-th and 88-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the 22
-nd day, so there is only one location
从小到大,依此假设每个点刚好是不动的点,然后每次假设当中都把一个地点放进一个带权并查集里,权值就是并查集里元素的个数。每次统计一下有多少个集合,集合里的元素个数是否相等,如果相等而且集合数变多了,就更新一次答案。
统计集合个数的时候,暴力把集合过一遍肯定是布星的,所以用哈希映射一下。
.
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
int n,fa[100005],sum[100005],a[100005];
struct node
{
int v,id;
}ns[100005];
bool cmp(node a,node b)
{
return a.v<b.v;
}
int find(int x)
{
int fx=fa[x];
if (fa[x]!=x)
{
fx=find(fa[x]);
}
return fa[x]=fx;
}
void u(int x,int y)
{
int fx=find(x);
int fy=find(y);
fa[fy]=fx;
sum[fx]+=sum[fy];
}
map<int,int> mapp;
int main()
{
scanf("%d",&n);
for (int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
node tmp; tmp.v=a[i]; tmp.id=i; ns[i]=tmp;
}
sort(ns+1,ns+1+n,cmp);
for (int i=1; i<=n; i++)
{
fa[i]=-1; sum[i]=1;
}
int ans; int maxx=0;
for (int i=1; i<=n; i++)
{
fa[ns[i].id]=ns[i].id;
int seq=ns[i].id;
if (seq+1<=n && fa[seq+1]!=-1)
{
int siz=sum[find(seq+1)];
mapp[siz]--;
if (mapp[siz]==0) mapp.erase(siz);
u(seq+1,seq);
}
if (seq-1>=1 && fa[seq-1]!=-1)
{
int siz=sum[find(seq-1)];
mapp[siz]--;
if (mapp[siz]==0) mapp.erase(siz);
u(seq-1,seq);
}
int tmp=sum[find(seq)];
if (mapp.count(tmp)==0) mapp[tmp]=1;
else mapp[tmp]++;
if (mapp.size()==1)
{
if (mapp[tmp]>maxx)
{
maxx=mapp[tmp];
ans=ns[i].v+1;
}
}
}
cout<<ans<<endl;
return 0;
}