Cod sursa(job #1377072)

Utilizator maribMarilena Bescuca marib Data 5 martie 2015 19:58:17
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>
#define DIM 100001
using namespace std;

int lung, x[DIM], before[DIM], temp[DIM], n, p;

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

void afisare(int ind)
{
    if(before[ind]!=0)
        afisare(before[ind]);
    out<<x[ind]<<" ";
}

int find_pos(int ind)
{
    int l=1, r=lung, mij;
    while(l<=r)
    {
        mij=(l+r+1)/2;
        if(x[temp[mij]]<x[ind])
        {
            l=mij+1;
        }
        else
        {
            r=mij-1;
        }
    }
    return l;
}


int main()
{
    in>>n;
    for(int i=1; i<=n; ++i) in>>x[i];
    temp[1]=1;
    lung=1;
    for(int i=2; i<=n; ++i)
    {
        p=find_pos(i);
        temp[p]=i;
        before[i]=temp[p-1];
        if(p>lung)
            lung=p;
    }
    out<<lung<<"\n";
    afisare(temp[lung]);
    in.close();
    out.close();
    return 0;
}