Cod sursa(job #2738783)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 6 aprilie 2021 12:39:52
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
#define debug(v,n) for (int i = 1; i <= (n); ++i) cout << v[i] << " ";
#define next cout << '\n'
#define LSB(a) ((-a) & a)
using namespace std;

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

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

int searchMax(int pos, int val) {
    int pMax = pos;
    while(pos > 0) {
        if(AIB[pMax] <= AIB[pos] && v[to[pos]] < val)
            pMax = pos;

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

void update(int pos, int val) {
    int idxStart = pos;
    while(pos <= n) {
        if(AIB[pos] < val) {

            AIB[pos] = val;
            to[pos] = idxStart;

        }

        pos += LSB(pos);
    }
}

int main() {
    //ifstream fin("date.in.txt");
    ifstream fin("scmax.in");
    ofstream fout("scmax.out");
    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]);

        to[x] = posSecvMax;

        AIB[x] = AIB[posSecvMax] + 1;

        update(x, AIB[x]);
    }
    fout << AIB[n];
    return 0;
}