Cod sursa(job #2744277)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 24 aprilie 2021 11:32:49
Problema Subsir crescator maximal Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.74 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 N = 200005;
int n, AIB[N];
int v[N];
set<int> vals;
map<int,int> idx;
int last[N];

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

void update(int pos, int act, int have) {
    while(pos <= n * 2) {
        if(last[pos] < act) {
            ++AIB[pos];
            have = max(AIB[pos], have);
            last[pos] = act;
        }
        else if(AIB[pos] <= have) {
            last[pos] = min(last[pos], act);
        }


        pos += LSB(pos);
    }
}

int preSecv(int pos) {
    int cnt = 0;
    while(pos > 0) {
        cnt += AIB[pos];
        pos -= LSB(pos);
    }
    return cnt;
}


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++;
    }

    for (int i = 1; i <= n; ++i) {
       // cout << v[i] << " " << idx[v[i]] << " " << preSecv(idx[v[i]]) + 1<< '\n';
        update(idx[v[i]], v[i], preSecv(idx[v[i]]) + 1);


        /*for (int j = 1; j <= n * 2; ++j)
            cout << AIB[j] << " ";
        cout << '\n';

        for (int j = 1; j <= n * 2; ++j)
            cout << last[j] << " ";
        cout << '\n';
        cout << '\n';
        */
    }

    int mx = 0;
    for (int i = 1; i <= n * 2; ++i)
        mx = max(mx, AIB[i]);
    #ifdef LOCAL
    cout << mx << '\n';
    #else
    fout << mx << '\n';
    #endif // LOCAL

    return 0;
}