Cod sursa(job #2610453)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 4 mai 2020 21:31:59
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.69 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 1e5;

template <typename T>
class InputReader {
    private:
        FILE *input_file ;
        static const int SIZE = (1 << 17) ;
        int point ;
        char buffer[SIZE] ;
        __attribute__ ( (always_inline)) void advance() {
            ++ point ;
            if (point == SIZE) {
                point = 0 ;
                fread (buffer, SIZE, 1, input_file) ;
            }
        }
    public:
        InputReader() {}
        InputReader (const char *file_name) {
            input_file = fopen (file_name, "r") ;
            point = 0 ;
            fread (buffer, SIZE, 1, input_file) ;
        }
        __attribute__ ( (always_inline)) InputReader &operator >> (T &n) {
            for (; !isdigit (buffer[point]) ; advance()) ;
            n = 0 ;
            for (; isdigit (buffer[point]) ; advance()) {
                n = n * ( (1 << 3) + (1 << 1)) + buffer[point] - '0' ;
            }
            return *this ;
        }
} ;
InputReader <int> in ("scmax.in");
ofstream out ("scmax.out");

int a[5 + N];

int lsb(int i) {
    return (i & (-i));
}

void Update(int pos, int val) {
    for(int i = pos; i <= N; i += lsb(i))
        a[i] = max(a[i], val);
}

int Query(int pos) {
    int ans(0);

    for(int i = pos; i >= 1; i -= lsb(i))
        ans = max(ans, a[i]);

    return ans;
}

pair <int, int> v[5 + N];
pair <int, int> cop[5 + N];
int dp[5 + N], sol[5 + N];

bool cmp (pair <int, int> a, pair <int, int> b) {
    return a.second < b.second;
}

int main() {
    //freopen("scmax.in", "r", stdin);
    //freopen("scmax.out", "w", stdout);
    int n, ans(0), cnt(0);
    //scanf("%d", &n);

    in >> n;

    for(int i = 1; i <= n; i++) {
        //scanf("%d", &v[i].first);
        in >> v[i].first;

        v[i].second = i;
        cop[i] = v[i];
    }

    sort(v + 1, v + n + 1);
    sort(cop + 1, cop + n + 1);

    for(int i = 1; i <= n; i++) {
        if(cop[i].first == cop[i - 1].first)
            v[i].first = v[i - 1].first;
        else v[i].first = 1 + v[i - 1].first;
    }

    sort(v + 1, v + n + 1, cmp);
    sort(cop + 1, cop + n + 1, cmp);

    for(int i = 1; i <= n; i++) {
        dp[i] = 1 + Query(v[i].first - 1);

        ans = max(ans, dp[i]);
        Update(v[i].first, dp[i]);
    }

    //printf("%d\n", ans);
    out << ans << '\n';


    for(int i = n; i >= 1; i--) {
        if(dp[i] == ans) {
            sol[++cnt] = cop[i].first;
            ans--;
        }
    }

    for(int i = cnt; i >= 1; i--)
        out << sol[i] << " ";
    //printf("%d ", sol[i]);
    out << '\n';
    //printf("\n");

    out.close();
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}