Cod sursa(job #2211075)

Utilizator calinmackCalin Manoli calinmack Data 9 iunie 2018 12:51:46
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <stack>
#include <fstream>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
const int maxx = 1e6+1;
int v[maxx],n,lg[maxx],lgmax,prev[maxx],i;
stack <int> s;
int main()
{
    in>>n;
    for(i=1;i<=n;i++)
        in>>v[i];
    for(i=1;i<=n;i++)
    {
        int st=1,dr=lgmax;
        while(st<=dr)
        {
            int mij=(dr+st)/2;
            if(v[lg[mij]]<v[i])
                st=mij+1;
            else
                dr=mij-1;
        }
            if(st>lgmax)
                lgmax=st;
            lg[st]=i;
            prev[i]=lg[dr];
    }
    int poz=lg[lgmax];
    while(poz!=0)
    {
        s.push(v[poz]);
        poz=prev[poz];
    }
    out<<lgmax<<'/n';
    while(!s.empty())
    {
        out<<s.top()<<' ';
        s.pop();
    }
    return 0;
}