Cod sursa(job #1735436)

Utilizator UrsuDanUrsu Dan UrsuDan Data 29 iulie 2016 20:41:34
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>

using namespace std;

int v[100010];
int st[100010];
int poz[100010];

int caut(int p,int u,int x)
{
    int m;
    while(p<=u)
    {
        m=(u+p)/2;
        if(x<v[st[m]])
            p=m+1;
        else
            u=m-1;
    }
    return p;
}

int main()
{
    int n,i,sf=0,pst=0;
    ifstream in ("scmax.in");
    ofstream out ("scmax.out");
    in>>n;
    for(i=1;i<=n;i++)
        in>>v[i];
    for(i=n;i>=1;i--)
    {
        pst=caut(1,sf,v[i]);
        if(pst>sf)
        {
            st[pst]=i;
            poz[i]=st[pst-1];
            sf=pst;
        }
        else
        {
            poz[i]=st[pst-1];
            if(v[st[pst]]<v[i])
                st[pst]=i;
        }
    }
    out<<sf<<endl;
    for(i=st[sf];i>0;i=poz[i])
        out<<v[i]<<" ";
    return 0;
}