Cod sursa(job #2748519)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 1 mai 2021 11:23:33
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 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) {
    int idxBest = pos;
    while(pos <= N) {

        if(path[pos].back() < val) {
            AIB[pos] += 1;
            path[pos].push_back(val);

            idxBest = pos;
        }
        else {
            if(AIB[pos] < AIB[idxBest]) {
                AIB[pos] = AIB[idxBest];
                path[pos] = path[idxBest];
            }
            else if(AIB[pos] == AIB[idxBest] && path[pos].back() > path[idxBest].back()) {
                path[pos] = path[idxBest];
            }
            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(idx[v[i]], 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;
}