Cod sursa(job #3167199)

Utilizator YosifIosif Andrei Stefan Yosif Data 10 noiembrie 2023 12:27:35
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.71 kb
#include <bits/stdc++.h>
using namespace std;

string file = "scmax";
ifstream fin(file + ".in");
ofstream fout(file + ".out");

int v[100001], s[100001], n;
int p[100001];

struct node {

    int lg = 0;
    int pos = 0;

    bool operator < (const node& aux) const {

        return lg < aux.lg;
    }

} aib[100001];

inline void add(int pos, node val) {

    for (int i = pos; i <= n; i += i & -i)
        if (aib[i] < val)
            aib[i] = val;
}

inline node get(int pos) {

    node ret;
    ret.lg = 0;

    for (int i = pos; i; i -= i & -i)
        if (ret < aib[i])
            ret = aib[i];

    return ret;
}

inline int getPos(int val) {

    int l = 1, r = n, pos = -1;

    while (l <= r) {

        int m = (l + r) >> 1;

        if (s[m] >= val) {

            pos = m;
            r = m - 1;
        }
        else
            l = m + 1;
    }

    return pos;
}

int main()
{
    fin >> n;

    for (int i = 1; i <= n; i++)
        fin >> v[i], s[i] = v[i];

    sort(s + 1, s + n + 1);

    int maxi = -1, resPos = -1;

    for (int i = 1; i <= n; i++) {

        int pos = getPos(v[i]);

        if (pos == 1) {

            if (1 > maxi) {

                maxi = 1;
                resPos = i;
            }
            add(pos, {1, i});
            continue;
        }

        node aux = get(pos - 1);

        p[i] = aux.pos;

        if (aux.lg + 1 > maxi) {

            maxi = aux.lg + 1;
            resPos = i;
        }

        add(pos, {aux.lg + 1, i});
    }

    fout << maxi << '\n';
    vector <int> res;

    while (resPos) {

        res.push_back(v[resPos]);
        resPos = p[resPos];
    }

    std::reverse(res.begin(), res.end());
    for (auto &i : res)
        fout << i << ' ';

    return 0;
}