Cod sursa(job #1944669)

Utilizator PaulStighiStiegelbauer Paul-Alexandru PaulStighi Data 29 martie 2017 10:50:37
Problema Subsir crescator maximal Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;

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

const int NMax = 100005;
const int oo = 1<<30;

int N,ND;
int A[NMax],D[NMax];

void Read()
{
    fin>>N;

    for(int i = 1 ; i <= N ; ++i)
        fin>>A[i];
}

void Solve()
{

    for(int i = 1 ; i <= NMax ; ++i)
        D[i] = oo;

    for(int i = 1 ; i <= N ; ++i)
    {
        int st = 1,dr = ND,pos = 0;

        while(st <= dr)
        {
            int mid = (st + dr) / 2;

            if(D[mid] < A[i])
            {
                pos = mid;
                st = mid + 1;
            }
            else    dr = mid - 1;
        }

        if(D[pos + 1] > A[i])
        {
            D[pos + 1] = A[i];
            if(pos + 1 > ND)    ND = pos + 1;
        }
    }
}

void Print()
{
    fout<<ND<<"\n";

    for(int i = 1 ; i <= ND ; ++i)
        fout<<D[i]<<" ";
}

int main()
{
    Read();
    Solve();
    Print();

    fin.close();
    fout.close();
    return 0;
}