Cod sursa(job #2052114)

Utilizator leraValeria lera Data 29 octombrie 2017 23:56:03
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <fstream>
#define Nmax 100005
using namespace std;

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

int a[Nmax], b[Nmax], N, lg, k, poz[Nmax];
int caut(int x, int y, int c)
{
    int ls = x;
    int ld = y;
    int mij;
    while(ls <= ld)
    {
        mij = ls + (ld - ls)/2;
        if(c < a[b[mij]])
            ls = mij + 1;
        else ld = mij - 1;
    }
    return ls ;
}
int main()
{
    fin >> N;
    for(int i = 1; i <= N; i++)
        fin >> a[i];
    a[0] = 1234567890;
    for(int i = N; i >= 1; i--)
    {
        k = caut(1, lg, a[i]);//vad unde s ar incadra a[i]
        poz[i] = 0;
       if(k > lg)
       {
           poz[i] = b[k - 1];
           lg = k;
           b[k] = i;
       }
       else
       {
           poz[i] = b[k - 1];
           if(a[b[k]] < a[i])b[k] = i;
       }
        //cel mai mare nr de numere din subsirul crescator maximal de la 1 pana la i - 1
    }
    fout << lg << '\n';
    for(int i = b[lg] ; i > 0; i = poz[i])fout << a[i] <<" ";
    return 0;
}