Cod sursa(job #1688930)

Utilizator razvan242Zoltan Razvan-Daniel razvan242 Data 13 aprilie 2016 20:07:11
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>

using namespace std;

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

const int NMAX = 1e5 + 1;

int n, a[NMAX], best[NMAX], daddy[NMAX];
int norm[NMAX], lennorm;

void recon(int i) {
    if (i == 0)
        return;
    recon(daddy[i]);
    fout << a[i] << ' ';
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> a[i];
    int step, ans;
    for (int i = 1; i <= n; ++i) {
        for (step = 1; (step << 1) <= lennorm; step <<= 1);
        ans = 0;
        for (int st = step; st; st >>= 1) {
            if (ans + st <= lennorm && a[norm[ans + st]] < a[i])
                ans += st;
        }
        best[i] = best[norm[ans]] + 1;
        daddy[i] = norm[ans];

        if (ans == lennorm)
            norm[++lennorm] = i;
        else
            if (a[norm[ans + 1]] > a[i])
                norm[ans + 1] = i;
    }
    fout << lennorm << '\n';
    for (int i = 1; i <= n; ++i) {
        if (best[i] == lennorm) {
            recon(i);
            return 0;
        }
    }
    return 0;
}