Cod sursa(job #2742704)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 21 aprilie 2021 16:16:14
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 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];
map<int,int> idx;
set<int> vals;

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

int countUnder(int pos) {
    int cnt = 0;

    while(pos > 0) {
        cnt += AIB[pos];

        if(AIB[pos] != 0) // in AIB tin cate elemente am de la [1 la N]
            return cnt;

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

void update(int pos, int secvAct) {
    while(pos <= n) {
        AIB[pos] = max(AIB[pos], secvAct);
        pos += LSB(pos);
    }
}


int main() {
    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++;

    for (int i = 1; i <= n; ++i) {
        if(idx[v[i]] == -1)
            continue;

        update(idx[v[i]], countUnder(idx[v[i]] - 1) + 1);

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

    int mx = 0;
    for (int i = 1; i <= n; ++i)
        mx = max(mx, AIB[i]);

    #ifdef LOCAL
    cout << mx << '\n';
    #else
    fout << mx << '\n';
    #endif // LOCAL

    return 0;
}