Cod sursa(job #2546538)

Utilizator CosminMorarMorar Cosmin Andrei CosminMorar Data 14 februarie 2020 11:40:50
Problema Subsir crescator maximal Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, lmax, nr;
int v[100001], sol[100001];
bool haveSol;

void copySol() {
    for (int i = 1; i <= lmax; i++)
        sol[i] = v[i];
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> nr;

        int st = 1, dr = lmax;
        while (st < dr) {
            int m = (st + dr + 1) / 2;
            if (v[m] <= nr)
                st = m;
            else
                dr = m - 1;
        }

        if (lmax == 0) {
            lmax++;
            v[lmax] = nr;
        } else {
            while (nr > v[st] && st < lmax)
                st++;

            if (nr > v[st]) {
                lmax++;
                v[lmax] = nr;
                copySol();
                haveSol = true;
            } else
                v[st] = nr;
        }
    }

    if (!haveSol)
        copySol();

    fout << lmax << '\n';
    for (int i = 1; i <= lmax; i++)
        fout << sol[i] << ' ';
    return 0;
}