Cod sursa(job #2747004)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 28 aprilie 2021 19:13:10
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 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

const int LIM = 2e5 + 15;
int n, AIB[LIM];
int v[LIM];
int actPosInVector[LIM];
map<int,int> idx;
set<int> vals;
vector<int> path[LIM];
int N;

void update(int pos, int val) {
    while(pos <= N) {

        if(path[pos].back() < val) {
            if(AIB[pos - LSB(pos)] < AIB[pos] + 1) {
                AIB[pos] += 1;
                path[pos].push_back(val);
            }
            else {
                AIB[pos] = AIB[pos - LSB(pos)];
                path[pos] = path[pos - LSB(pos)];
            }
        }
        else {
            if(AIB[pos] < AIB[pos - LSB(pos)]) {
                AIB[pos] = AIB[pos - LSB(pos)];
                path[pos] = path[pos - LSB(pos)];
            }
            else
                return;
        }
        pos += LSB(pos);
    }
}

int main() {
    read();
    fin >> n;

    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        vals.insert(v[i]);
    }
    int p = 1;
    for (int x : vals) {
        idx[x] = p++;
    }
    N = 1;
    while(N < p)
        N *= 2;

    for (int i = 1; i <= N; ++i)
        path[i].push_back(0);

    for (int i = 1; i <= n; ++i) {
        update(1, v[i]);

       /* for (int i = 1; i <= N; ++i)
            cout << AIB[i] << " ";
        cout << '\n';
        */
    }

    #ifdef LOCAL
    cout << AIB[N] << '\n';

    for (int i = 1; i < sz(path[N]); ++i)
        cout << path[N][i] << " ";
    #else
    fout << AIB[N] << '\n';

    for (int i = 1; i < sz(path[N]); ++i)
        fout << path[N][i] << " ";
    #endif // LOCAL

    return 0;
}