Cod sursa(job #3369300)

Utilizator Belea_DariusBelea Mihai Darius Belea_Darius Data 19 octombrie 2026 16:00:58
Problema Subsir crescator maximal Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.7 kb
#include <bits/stdc++.h>
#define MAXN 100000

using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");

int v[MAXN + 1], old_v[MAXN + 1], aib[MAXN + 1], lung[MAXN + 1];

struct per {
    int val, ind;
}sorted[MAXN + 1];
int cmp(per A, per B) {
    if (A.val < B.val) return 1;
    return A.ind < B.ind;
}

void norm(int n) {
    int i, curr;

    sort(sorted + 1, sorted + n + 1, cmp);

    curr = 0;
    for (i = 1; i <= n; i++) {
        if (sorted[i].val > sorted[i - 1].val) {
            curr++;
        }
        v[sorted[i].ind] = curr;
    }
}

int lsb(int x) {
    return (x & (-x));
}
void update(int n, int poz, int val) {
    while (poz <= n) {
        aib[poz] = max(aib[poz], val);
        poz += lsb(poz);
    }
}
int query(int poz) {
    int ans = 0;
    while (poz > 0) {
        ans = max(ans, aib[poz]);
        poz -= lsb(poz);
    }
    return ans;
}

void refac(int poz, int lung_need, int val_need) {
    if (lung_need > 0) {
        if (lung[poz] == lung_need && old_v[poz] <= val_need) {
            refac(poz - 1, lung_need - 1, val_need - 1);
            fout << old_v[poz] << " ";
        }else {
            refac(poz - 1, lung_need, val_need);
        }
    }
}

int main() {
    int n, i, cur_len, poz_max;

    fin >> n;
    for (i = 1; i <= n; i++) {
        fin >> old_v[i];
        sorted[i].val = old_v[i];
        sorted[i].ind = i;
    }
    norm(n);

    poz_max = 1;
    for (i = 1; i <= n; i++) {
        cur_len = query(v[i] - 1) + 1;
        lung[i] = cur_len;
        if (lung[i] > lung[poz_max]) {
            poz_max = i;
        }
        update(n, v[i], cur_len);
    }
    fout << lung[poz_max] << "\n";
    refac(poz_max, lung[poz_max], old_v[poz_max]);
    return 0;
}