Cod sursa(job #3350651)

Utilizator Alex_at_gameIustin-Alexandru Frateanu Alex_at_game Data 11 aprilie 2026 16:21:07
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(v) begin(v), end(v)
#define al(v, l, r) begin(v) + l, begin(v) + r + 1
#define sz(v) (int)v.size()
#define pb push_back
#define pob pop_back
#define fs first
#define sd second

constexpr int inf = 2e9;
constexpr ll infll = 4e18;
constexpr int N = 1e5 + 5;

struct aib {
    int n;
    vector<int> f;

    aib(int n): n(n), f(n + 1, 0) {}

    inline void add(int i, int x) {
        for (; i <= n; i += (i & -i)) {
            f[i] = max(f[i], x);
        }
    }

    inline int mx(int i) {
        int ans = 0;
        for (; i; i -= (i & -i)) {
            ans = max(ans, f[i]);
        }

        return ans;
    }
};

int n, a[N], dp[N];
signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);

    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }

    vector<int> v;
    for (int i = 1; i <= n; ++i) {
        v.pb(a[i]);
    }

    sort(all(v));
    v.erase(unique(all(v)), end(v));

    for (int i = 1; i <= n; ++i) {
        a[i] = lower_bound(all(v), a[i]) - begin(v) + 1;
    }

    aib fen(n);
    for (int i = 1; i <= n; ++i) {
        dp[i] = fen.mx(a[i]) + 1;
        fen.add(a[i], dp[i]);
    }

    int poz = *max_element(al(dp, 1, n));
    vector<int> ans;

    for (int i = n; i; --i) {
        if (dp[i] == poz) {
            ans.pb(a[i]);
            poz--;
        }
    }

    reverse(all(ans));
    ans.erase(unique(all(ans)), end(ans));
    cout << sz(ans) << "\n";

    for (int x : ans) {
        cout << v[x - 1] << " ";
    }

    cout << "\n";
}