Cod sursa(job #2532910)

Utilizator vladadAndries Vlad Andrei vladad Data 28 ianuarie 2020 16:30:29
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int n, v[200001], poz[200001], d[200001], ans[200001], b, l, maxim, maxim2;
int cb(int x, int dr)
{
    int st=1, anscb=0, mid;
    while(st<=dr)
    {
        mid=(st+dr)/2;
        if(v[poz[mid]]<x)
        {
            anscb=mid;
            st=mid+1;
        }
        else
            dr=mid-1;
    }
    return anscb;
}
int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
        f>>v[i];
    for(int i=1; i<=n; i++)
    {
        b=cb(v[i], l);
        l=max(l, b+1);
        d[i]=b+1;
        poz[d[i]]=i;
        maxim=max(maxim, d[i]);
    }
    maxim2=maxim;
    g<<maxim<<'\n';
    for(int i=n; i>=1; i--)
    {
        if(maxim2==d[i])
        {
            ans[maxim2]=v[i];
            maxim2--;
        }
    }
    for(int i=1; i<=maxim; i++)
    g<<ans[i]<<' ';
    return 0;
}