Cod sursa(job #2734159)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 31 martie 2021 14:45:05
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.6 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], idx[N];

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

int check(int pos) {
    int maxDone = 0;
    while(pos > 0) {
        //cout << pos - LSB(pos) + 1 << " " << pos << " " << AIB[pos]<< '\n';
        maxDone = max(maxDone, AIB[pos]);
        pos -= LSB(pos);
    }
    return maxDone;
}

void update(int pos, int x) {
    while(pos <= n) {
        //cout << pos - LSB(pos) + 1 << " " << pos << '\n';
        if(idx[pos] < x && v[idx[pos]] < v[x]) {
            idx[pos] = x;
            ++AIB[pos];
        }

        pos += LSB(pos);
    }
}

int main() {
    //ifstream fin("date.in.txt");
    ifstream fin("scmax.in");
    ofstream fout("scmax.out");
    fin >> n;

    vector<int> pos;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        pos.push_back(i);
    }
    sort(pos.begin(), pos.end(), cmp);

    for (int x : pos) {
        int ad = max(AIB[x], AIB[x - LSB(x)]);
        //cout << x << " " << v[x] << "\n";
        update(1, x);


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

  //  for (int i = 1; i <= n; ++i)
    //    cout << atIdx[i] << " ";

    return 0;
}