Cod sursa(job #2671657)

Utilizator CiubarLoverBaiatu cu Ciubaru CiubarLover Data 12 noiembrie 2020 15:23:22
Problema Subsir crescator maximal Scor 55
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
vector<int>seq;
int n,x,lmax;
int where[100005];
int v[100005];
int binar(int pos1,int pos2,int nr)
{
    ///cel mai mare index cu o valoare mai mica decat nr
    while(pos2>pos1)
    {
        int mid=(pos2+pos1)/2;
        if (seq[mid]>=nr)
            pos2=mid;
        else
            pos1=mid+1;
    }
    return pos2;
}
int main()
{
    fin>>n;
    fin>>v[1];
    seq.push_back(v[1]);
    lmax=1;
    where[1]=1;
    for(int i=2;i<=n;i++)
    {
        fin>>v[i];
        int index=binar(0,lmax,v[i]);
        where[i]=index+1;
        if(index==0)
        {
            seq[0]=v[i];
            continue;
        }
        if(index==lmax)
        {
            lmax++;
            seq.push_back(v[i]);
            continue;
        }
        seq[index]=v[i];
    }
    fout<<lmax<<"\n";
    int pos=lmax;
    int i=n;
    while(pos!=0)
    {
        if (where[i]==pos)
        {
            pos--;
            fout<<v[i]<<" ";
        }
        i--;
    }
    return 0;
}