Cod sursa(job #2961259)

Utilizator flibiaVisanu Cristian flibia Data 6 ianuarie 2023 01:29:10
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.69 kb
#include <bits/stdc++.h>

using namespace std;
 
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 69
#define nl(...) 42
#endif

#define ll long long
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random(ll st, ll dr) {
    assert(st <= dr);
    return st + rng() % (dr - st + 1);
}

const int MOD = 1e9 + 7;
const int N = 2e5 + 10;

vector<int> dp;
vector<int> pv;

int combine(int x, int y) {
    if (dp[x] > dp[y]) {
        return x;
    } else {
        return y;
    }
}

struct Node {
    int val;
    int leftMargin;
    int rightMargin;  
    Node *left;
    Node *right;
    Node(int val, int l, int r): val(val), leftMargin(l), rightMargin(r), left(nullptr), right(nullptr) {}

    bool isLeaf() {
        return leftMargin == rightMargin;
    }

    int mid() {
        return leftMargin + (rightMargin - leftMargin) / 2;
    }

    void update() {
        int l = 0, r = 0;
        if (left) {
            l = left->val;
        }
        if (right) {
            r = right->val;
        }
        
        val = combine(l, r);
    }
};

struct SegTreeDynamic {
    Node *root;
    SegTreeDynamic(int l, int r) {
        root = new Node(0, l, r);
    }

    void update(Node *node, int id, int val) {
        if (id > node->rightMargin || id < node->leftMargin) {
            return;
        }

        if (node->isLeaf()) {
            node->val = combine(node->val, val);
            return;
        }

        if (id <= node->mid()) {
            if (node->left == nullptr) {
                node->left = new Node(0, node->leftMargin, node->mid());
            }
            update(node->left, id, val);
        } else {
            if (node->right == nullptr) {
                node->right = new Node(0, node->mid() + 1, node->rightMargin);
            }
            update(node->right, id, val);
        }

        node->update();
    }

    void update(int id, int val) {
        update(root, id, val);
    }

    int query(Node *node, int l, int r) {
        if (!node || r < node->leftMargin || l > node->rightMargin) {
            return 0;
        }

        if (l <= node->leftMargin && node->rightMargin <= r) {
            return node->val;
        }

        int leftV = query(node->left, l, r); 
        int rightV = query(node->right, l, r);

        return combine(leftV, rightV);
    }

    int query(int l, int r) {
        return query(root, l, r);
    }
};

void solve(int test, istream &cin, ostream &cout) {
    int n;
    cin >> n;
    vector<int> a(n + 1, 0);
    dp = vector<int>(n + 1, 0);
    pv = vector<int>(n + 1, 0);

    SegTreeDynamic sgt(1, 2e9);
    
    int sol = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        int best = sgt.query(0, a[i] - 1);
        dp[i] = dp[best] + 1;
        pv[i] = best;
        if (dp[i] > dp[sol]) {
            sol = i;
        }
        sgt.update(a[i], i);
    }

    cout << dp[sol] << '\n';
    
    auto rec = [&](auto &&rec, int i) {
        if (!i) {
            return;
        }

        rec(rec, pv[i]);
        cout << a[i] << ' ';
    };

    rec(rec, sol);
}

int main() {
    ifstream cin("scmax.in");
    ofstream cout("scmax.out");
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    auto start = std::chrono::high_resolution_clock::now();

    bool multiTest = false;

    int t;    
    if (multiTest) {
        cin >> t;
    } else {
        t = 1;
    }

    for (int test = 1; test <= t; test++) {
        solve(test, cin, cout);
    }

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
    // cerr << fixed << setprecision(6) << "Running time: " << (double) duration.count() / 1e6 << " seconds" << '\n';

    return 0;
}