982D codeforces - Shark




982D codeforces - Shark







Solution in C++



#include <bits/stdc++.h>
using namespace std;

//Hello World

typedef long long int lli;
typedef long double ld;
#define pii pair<int,int>
#define piiti pair< pair<int,int>,int>
#define ipii pair<int,pair<int,int> >
#define mod 1000000007
#define lasB(b) (b&(-b))
#define N 100005

int fat[N];
vector<pii> all;
int n , t1 , t2 , per , tot , big , ans , bot;

map<int , int> sege , tal;

int fin(int x)
{
    if(x!=fat[x])fat[x]=fin(fat[x]);
    return fat[x];
}

void unio(int a , int b)
{
    int fa = fin(a);
    int fb =  fin(b);
    fat[fb]=fat[fa];
    sege[fa]+=sege[fb];
    sege[fb]=0;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie();
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t1;
        all.push_back({t1,i});
        fat[i]=i;
    }
    sort(all.begin(),all.end());
    for(int i=0;i<n;i++)
    {
        int x =all[i].first , y = all[i].second;
        sege[y]++;
        int sb = (y==0?0:sege[fin(y-1)]) , sa= (y==n-1?0:sege[fin(y+1)]);
        big = max(big , sb + sa +1);
        if(sb && sa)tot--;
        else if(!sb && !sa)tot++;
        tal[sb+sa+1]++;
        if(sb)tal[sb]--;
        if(sa)tal[sa]--;
        if(tal[big] >= tot && tot>bot)
            ans = x+1 , bot=tot;
        if(y-1>=0 && sb)unio(y , y-1);
        if(y+1<n && sa)unio(y , y+1);
    }
    cout<<ans;
    return 0;
}
Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق