Cod sursa(job #2267068)

Utilizator mihai002016Zaharia Teodor Mihai mihai002016 Data 23 octombrie 2018 10:36:06
Problema Subsir crescator maximal Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int a[100005],poz[100005],n,best[100005],lgbest,i,j,pozitie,cine,s[100005];
int cauta (int x);
int main()
{
    fin>>n;
    for(i=1; i<=n; i++)
        fin>>a[i];
    best[++lgbest]=a[1];
    for(i=2; i<=n; i++)
    {
        if(a[i]>best[lgbest])
        {
            best[++lgbest]=a[i];
            poz[i]=lgbest;
        }
        else
        {
            pozitie=cauta(a[i]);
            poz[i]=pozitie;
            best[pozitie]=a[i];
        }
    }
    fout<<lgbest<<'\n';
    cine=lgbest;
    for(i=n;i>=1;i--)
    {
        if(poz[i]==cine)
        {
            s[cine]=a[i];
            cine--;
        }
    }
    for(i=1;i<=lgbest;i++)
        fout<<s[i]<<" ";
    return 0;
}
int cauta (int x)
{
    int mid,st ,dr;
    st=0;
    dr=lgbest+1;
    while(dr-st>1)
    {
        mid=(st+dr)/2;
        if(best[mid]>=x)
        {
            dr=mid;
        }
        else
        st=mid;
    }
    return dr;
}