Cod sursa(job #2610448)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 4 mai 2020 21:23:07
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.69 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#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;

class Fenwick {
    private:
        int a[5 + N];

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

    public:
        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;
        }
};
Fenwick aib;

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);

    for(int i = 1; i <= n; i++) {
        scanf("%d", &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 + aib.Query(v[i].first - 1);

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

    printf("%d\n", ans);


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

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

    fclose(stdin);
    fclose(stdout);
    return 0;
}