Cod sursa(job #2741653)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 17 aprilie 2021 10:55:02
Problema Subsir crescator maximal Scor 65
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
read();

const int N = 100005;
int n, AIB[N];
int v[N];
int actPosInVector[N];
vector<int> path[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[actPosInVector[pos]] < val)
            pMax = pos;

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

void update(int pos, int val, int actAdded, vector<int> &secv) {
    while(pos <= n) {

        if(AIB[pos] < val) {
            AIB[pos] = val;
            actPosInVector[pos] = actAdded;
            path[pos] = secv;
        }

        pos += LSB(pos);
    }
}


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]);
        vector<int> secv = path[posSecvMax];
        secv.push_back(v[x]);
        path[x] = secv;

        update(x, AIB[posSecvMax] + 1, x, secv);
/*
        for (int i = 1; i <= n; ++i) {
            cout << AIB[i] << " ";
        }
        cout << '\n';
        for (int i = 1; i <= n; ++i) {
            cout << actPosInVector[i] << " ";
        }
        cout << '\n';
        for (int i = 1; i <= n; ++i) {
            cout << from[i] << " ";
        }
        cout << '\n' << '\n';
  */
    }

    int best = searchMax(n, INT_MAX);
    fout << AIB[best] << '\n';
    for (int x : path[best])
        fout << x << " ";

    return 0;
}