Cod sursa(job #2707224)

Utilizator vladstefanVlad Oros vladstefan Data 16 februarie 2021 18:05:06
Problema Subsir crescator maximal Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb

// Problema scmax - subsir crescator maximal

#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>

#define NMax 100000

using namespace std;

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

int n;
vector<int> v(NMax + 3), q(NMax + 3), aux(NMax + 3);

void input() {
    v.resize(1);
    q.resize(1);
    aux.resize(1);
    fin >> n;
    int x;
    for (int i = 1; i <= n; ++i) {
        fin >> x;
        v.push_back(x);
    }
}

void init() {
    for (int i = 1; i <= n; ++i) {
        if (v[i] > q[q.size() - 1]) q.push_back(v[i]);
        else q[(lower_bound(q.begin(), q.end(), v[i])) - q.begin()] = v[i];
        aux[i] = q.size();
    }
}

void solve() {
    int t = NMax + 3;
    for (int i = n; i; --i) if (aux[i] < t) {
        t = aux[i];
        aux[i] = 0;
    }
    fout << q.size() - 1 << '\n';
    for (int i = 1; i <= n; ++i) if (!aux[i]) fout << v[i] << ' ';
}

int main() {
    input();
    init();
    solve();
}