Cod sursa(job #2206310)

Utilizator AplayLazar Laurentiu Aplay Data 22 mai 2018 11:11:16
Problema Subsir crescator maximal Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <cstdio>
#include <iostream>

#define NMAX 100010

using namespace std;

int n, v[NMAX], sSize, s[NMAX], ans, pAns, prevv[NMAX];

int bSearch(int searched, int sSize, int* s) {
    int left = 0, right = sSize - 1;
    while (left < right) {
        int middle = left + (right - left) / 2;
        if (v[searched] <= v[s[middle]]) right = middle;
        else  left = middle + 1;
    }
    return left;
}

void printSolution(int position) {
    if (-1 == prevv[position]) {
        cout << v[position] << " ";
        return;
    }

    printSolution(prevv[position]);
    cout << v[position] << " ";
}

int main() {
    freopen("scmax.in", "r" , stdin);
    freopen("scmax.out", "w", stdout);

    cin >> n;
    for (int it = 0; it < n; ++it) {
        cin >> v[it];
    }

    ans = sSize = 1;
    s[0] = 0;
    prevv[0] = -1;
    for (int it = 1; it < n; ++it) {
        int bsIndex = bSearch(it, sSize, s);
        if (bsIndex == sSize - 1 && v[s[bsIndex]] < v[it]) {
            s[sSize++] = it;
            ++bsIndex;
            prevv[it] = 0 == bsIndex ? -1 : s[bsIndex - 1];
        } else {
            prevv[it] = prevv[s[bsIndex]];
            s[bsIndex] = it;
        }

        if (ans < bsIndex + 1) {
            pAns = it;
            ans = bsIndex + 1;
        }
    }

    cout << ans << endl;
    printSolution(pAns);

    return 0;
}