Cod sursa(job #2610591)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 5 mai 2020 09:21:39
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.52 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <fstream>
#include <algorithm>

using namespace std;

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

class OutputReader {
private:
    FILE *fout;
    char *buff;
    int sp;
    static const int SIZE = (1 << 17);

    void write_ch(char ch) {
        if (sp == SIZE) {
            fwrite(buff, 1, SIZE, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutputReader(const char* name) {
        fout = fopen(name, "w");
        buff = new char[SIZE]();
        sp = 0;
    }
    ~OutputReader() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutputReader& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutputReader& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutputReader& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutputReader& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

InputReader <int> in ("scmax.in");
OutputReader out ("scmax.out");

int a[5 + N];

int Max(int a, int b) {
    if(a > b) return a;
    return b;
}

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

struct ura {
    int val, norm, poz;
};
ura v[5 + N];
int dp[5 + N], sol[5 + N];

bool cmp1 (ura a, ura b) {
    return a.val < b.val;
}

bool cmp2 (ura a, ura b) {
    return a.poz < b.poz;
}

int main() {
    int n, ans(0), cnt(0);

    in >> n;

    for(int i = 1; i <= n; i++) {
        in >> v[i].val;

        v[i].poz = i;
    }

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

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

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

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

        ans = Max(ans, dp[i]);
        Update(v[i].norm, dp[i]);
    }

    out << ans << '\n';


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

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

    return 0;
}