Cod sursa(job #2740621)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 13 aprilie 2021 17:26:36
Problema Subsir crescator maximal Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.77 kb
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
#define LSB(a) ((-a) & a)
using namespace std;

#ifdef LOCAL
#define read() ifstream fin("date.in.txt");
#else
#define read() ifstream fin("scmax.in"); ofstream fout("scmax.out");
#endif // LOCAL
read();

const int N = 100005;
int n, AIB[N];
int v[N];
int from[N];
int bottom[N];

bool cmp(int a, int b) {
    return v[a] < v[b];
}

int searchMax(int pos, int val) {
    int pMax = 0;
    while(pos > 0) {

        if(AIB[pMax] <= AIB[pos] && v[from[pos]] < val)
            pMax = pos;

        pos -= LSB(pos);
    }
    return pMax;
}

void update(int pos, int val) {
    int actAdded = pos;
    while(pos <= n) {

        if(AIB[pos] < val) {
            AIB[pos] = val;
            from[pos] = actAdded;
        }

        pos += LSB(pos);
    }
}

void compose(int act) {
    if(bottom[from[act]] != 0)
        compose(bottom[from[act]]);
    fout << v[from[act]] << " ";
}

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

    vector<int> p(n);
    iota(p.begin(), p.end(), 1);
    sort(p.begin(), p.end(), cmp);

    for (int x : p) {
        int posSecvMax = searchMax(x, v[x]);

        bottom[x] = from[posSecvMax];

        update(x, AIB[posSecvMax] + 1);
        /*for (int i = 1; i <= n; ++i) {
            cout << AIB[i] << " ";
        }
        cout << '\n';
        for (int i = 1; i <= n; ++i) {
            cout << from[i] << " ";
        }
        cout << '\n';
        for (int i = 1; i <= n; ++i) {
            cout << bottom[i] << " ";
        }
        cout << '\n' << '\n';
*/
    }
    int best = searchMax(n, INT_MAX);
    fout << AIB[best] << '\n';
    compose(best);
    return 0;
}