Cod sursa(job #1649991)

Utilizator tudormaximTudor Maxim tudormaxim Data 11 martie 2016 16:05:18
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin ("scmax.in");
ofstream fout ("scmax.out");

const int nmax = 1e5+5;
int dp[nmax], dad[nmax], v[nmax], maxx;

inline int bsearch(int val)
{
    int st=1, dr=maxx, mij;
    while(st<=dr)
    {
        mij=(st+dr)>>1;
        if(v[dp[mij]] < val) st=mij+1;
        else dr=mij-1;
    }
    return st;
}

void secventa(int nod)
{
    if(dad[nod]) secventa(dad[nod]);
    fout << v[nod] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, i, poz;
    fin >> n;
    for(i=1; i<=n; i++)
        fin >> v[i];
    for(i=1; i<=n; i++)
    {
        poz=bsearch(v[i]);
        maxx=max(maxx, poz);
        dp[poz]=i;
        dad[i]=dp[poz-1];
    }
    fout << maxx << "\n";
    secventa(dp[maxx]);
    fin.close();
    fout.close();
    return 0;
}