Cod sursa(job #2299940)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 10 decembrie 2018 16:30:31
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
using namespace std;

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

const int MAXN = 100000 + 16;
int N, K;
int A[MAXN], DP[MAXN], T[MAXN];

void printout(int pos)
{
    if(!pos) return;

    printout(T[pos]);
    fout << A[pos] << ' ';
}

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

    DP[K = 1] = 1;
    for(int i = 2; i <= N; ++i)
    {
        int st = 1, dr = K;
        while(st <= dr)
        {
            int m = st + (dr - st) / 2;
            if(A[DP[m]] < A[i])
                st = m + 1;
            else
                dr = m - 1;
        }
        if(st > K)
            K++;
        DP[st] = i;
        T[i] = DP[st - 1];
    }

    fout << K << '\n';
    printout(DP[K]);

    return 0;
}