CodeForces 982D Shark 并查集+哈希

这篇博客讨论了生物学家Max如何通过观察鲨鱼的移动距离来确定其访问的地点数量。文章介绍了一个问题,即如何选择阈值kk,使得鲨鱼在每个地点停留的天数相同且地点数量最大化。解决方案涉及使用并查集来跟踪不同地点,并利用哈希映射优化集合计数过程。通过从小到大遍历所有可能的kk值,找到满足条件的最大地点数和最小kk值。
D. Shark
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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

The second line contains nn distinct positive integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the distance traveled in each of the day.

Output

Print a single integer kk, such that

  1. the shark was in each location the same number of days,
  2. the number of locations is maximum possible satisfying the first condition,
  3. kk is smallest possible satisfying the first and second conditions.
Examples
input
Copy
8
1 2 7 3 4 8 5 6
output
Copy
7
input
Copy
6
25 1 2 3 14 36
output
Copy
2
Note

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值