Cod sursa(job #1691122)

Utilizator deliricnagisa deliric Data 16 aprilie 2016 22:06:30
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

//#define DEBUG

struct Debug {
    template <typename _type>
    inline Debug operator << (_type a) {
        #ifdef DEBUG
            cout << a;
        #endif
    }
} debug;

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

const int MAXN = 1e5 + 5;
int n, res, pos, dp[MAXN];
int a[MAXN], v[MAXN];

int norm[MAXN];
inline int search(const int x) {
    int i = 0, cnt = 1 << 17;
    for (; cnt > 0; cnt = cnt >> 1) {
        if (i + cnt <= n && norm[i + cnt] <= x) {
            i += cnt;
        }
    }
    return i;
}

int bit[MAXN];
inline void update(const int p, const int x) {
    for (int i = p; i <= n; i = (i | (i - 1)) + 1) {
        bit[i] = max(bit[i], x);
    }
}
inline int query(const int p) {
    int r = 0;
    for (int i = p; i >= 1; i = i & (i - 1)) {
        r = max(r, bit[i]);
    }
    return r;
}

inline void print(const int p, const int r) {
    if (p == 0) {
        return;
    }
    debug << p << ' ' << r << '\n';
    if (dp[p] + 1 == r) {
        print(p - 1, r - 1);
        fout << v[p] << ' ';
    } else {
        print(p - 1, r);
    }
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        fin >> a[i];
        v[i] = a[i];
        norm[++norm[0]] = a[i];
    }
    sort(norm + 1, norm + 1 + n);
    for (int i = 1; i <= n; ++i) {
        a[i] = search(a[i]);
    }
    for (int i = 1; i <= n; ++i) {
        int now = query(a[i] - 1) + 1;
        update(a[i], now);
        dp[i] = now;
        if (res < now) {
            res = now;
            pos = i;
        }
    }
    fout << res << '\n';
    print(pos, res);
    fout << v[pos] << '\n';
    fout.close();
    return 0;
}