Cod sursa(job #1897140)

Utilizator danyvsDan Castan danyvs Data 1 martie 2017 10:30:43
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>

using namespace std;

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

const int NMAX = 100005;

int V[NMAX], n;
int par[NMAX], best[NMAX], lgmax;
int L[NMAX];

void read() {
    fin >> n;
    for (int i = 1; i <= n; ++ i)
        fin >> V[i];
}

int binarySearch(int target) {
    int lo = 0, hi = lgmax;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (V[L[mid]] < target && V[L[mid + 1]] >= target)
            return mid;
        else
            if (V[L[mid + 1]] < target)
                lo = mid + 1;
            else
                hi = mid - 1;
    }
    return lgmax;
}

void dp() {
    lgmax = 1;
    L[0] = 0;
    best[1] = L[1] = 1;
    for (int i = 2; i <= n; ++ i) {
        int pos = binarySearch(V[i]);
        par[i] = L[pos];
        best[i] = pos + 1;
        L[pos + 1] = i;
        if (lgmax < pos + 1)
            lgmax = pos + 1;
    }
}

void sol(int pos) {
    if (!pos)
        return;
    sol(par[pos]);
    fout << V[pos] << " ";
}

void print() {
    fout << lgmax << "\n";
    for (int i = 1; i <= n; ++ i)
        if (best[i] == lgmax) {
            sol(i);
            return;
        }
}

int main() {
    read();
    fin.close();
    dp();
    print();
    fout.close();
    return 0;
}