Cod sursa(job #2153408)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 6 martie 2018 10:24:56
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("scmax.in");
ofstream g("scmax.out");

int N, k, MAX, poz, a[100003], p[100003], st[100003];

int cautbin(int x);

void reconst(int poz, int max);

int main() {

    f >> N;
    for (int i = 1; i <= N; ++i) {
        f >> a[i];
        int pozt = cautbin(a[i]);
        st[(k = pozt)] = a[i];
        p[i] = k;
        if(MAX < p[i]) MAX = p[i], poz = i;
    }
    g << MAX << "\n";
    reconst(poz, MAX);
    g << "\n";
    return 0;
}

void reconst(int poz, int max) {
    if(poz < 1) return;
    if(p[poz] == max) {
        reconst(poz - 1, max - 1);
        g << a[poz] << " ";
    }
    else
        reconst(poz - 1, max);
}

int cautbin(int x) {
    int s = 1, d = k;
    while(s <= d) {
        int mij = (s + d) / 2;
        if(st[mij] == x)
            break;
        else if (st[mij] < x)
            s = mij + 1;
        else
            d = mij - 1;
    }
    return s;
}