Cod sursa(job #2605264)

Utilizator As932Stanciu Andreea As932 Data 24 aprilie 2020 17:55:49
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");

const int nmax=100005;

int n,a[nmax],lst[nmax],res[nmax],up[nmax],dp[nmax],aib[nmax],best;

void read()
{
    fin>>n;

    for(int i=1;i<=n;i++)
    {
        fin>>a[i];

        res[i]=lst[i]=a[i];
    }
}

void update(int poz,int ind)
{
    for(;poz<=n;poz+= poz & -poz)
        if(dp[ind]>dp[aib[poz]])
            aib[poz]=ind;
}

int query(int poz)
{
    int mx=0;
    for(;poz>0;poz-= poz & -poz)
        if(dp[aib[poz]]>dp[mx])
            mx=aib[poz];
    return mx;
}

int main()
{
    read();

    sort(lst+1,lst+n+1);

    int h=0;

    for(int i=1;i<=n;i++)//elimina elementele care sunt identice si consecutive in vectorul sortat
        if(lst[i]!=lst[i+1])
            lst[++h]=lst[i];

    for(int i=1;i<=n;i++)
        a[i]=lower_bound(lst+1,lst+h+1,a[i])-lst;

    for(int i=1;i<=n;i++)
    {
        up[i]=query(a[i]-1);

        dp[i]=dp[up[i]]+1;

        update(a[i],i);
    }

    for(int i=1;i<=n;i++)
        if(dp[best]<dp[i])
            best=i;

    fout<<dp[best]<<"\n";

    h=0;
    for(int i=best;i>0;i=up[i])
        lst[++h]=res[i];

    for(;h>0;--h)
        fout<<lst[h]<<" ";

    return 0;
}