Cod sursa(job #2029140)

Utilizator al.mocanuAlexandru Mocanu al.mocanu Data 29 septembrie 2017 15:33:55
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <algorithm>
#define zeros(x) (x & -x)
#define MAX 100005
using namespace std;

int n, aib[MAX], a[MAX], l[MAX], v[MAX], d[MAX], p[MAX];

void update(int pos, int val) {
    for(int i = pos; i <= n; i += zeros(i))
        if(d[aib[i]] < d[val])
            aib[i] = val;
}

int query(int pos) {
    int res = 0;
    for(int i = pos; i > 0; i -= zeros(i))
        if(d[res] < d[aib[i]])
            res = aib[i];
    return res;
}

void print(ofstream& g, int pos) {
    if(pos == 0)
        return;
    print(g, p[pos]);
    g << a[pos] << " ";
}

int main() {
    ifstream f("scmax.in");
    ofstream g("scmax.out");
    f >> n;
    for(int i = 1; i <= n; ++i) {
        f >> a[i];
        l[i] = a[i];
    }
    sort(l + 1, l + n + 1);

    int h = 1;
    for(int i = 2; i <= n; ++i)
        if(l[i] != l[h])
            l[++h] = l[i];

    for(int i = 1; i <= n; ++i)
        v[i] = lower_bound(l + 1, l + h + 1, a[i]) - l;

    for(int i = 1; i <= n; ++i) {
        p[i] = query(v[i] - 1);
        d[i] = d[p[i]] + 1;
        update(v[i], i);
    }

    int res = 0;
    for(int i = 1; i <= n; ++i)
        if(d[res] < d[i])
            res = i;
    g << d[res] << "\n";
    print(g, res);
    return 0;
}