Cod sursa(job #2287197)

Utilizator LittleWhoFeraru Mihail LittleWho Data 21 noiembrie 2018 17:25:32
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

const int max_n = 100000;
int n;
vector<int> v(max_n);
vector<int> before(max_n);
vector<int> tail(max_n);
vector<int> solution(max_n);
int length, last_position;

int main() {
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        tail[i] = 0;
        before[i] = -1;
    }
    
    for (int i = 1; i < n; i++) {
        if (v[i] < v[tail[0]]) {
            tail[0] = i;
        } else if (v[i] > v[tail[length]]) {
            tail[++length] = i;
            before[i] = tail[length - 1];
            last_position = i;
        } else {
            auto pos = lower_bound(
                tail.begin(),
                tail.begin() + length + 1,
                i,
                [](int a, int b) {
                    return v[a] < v[b];
                }
            );

            tail[*pos] = i;
            before[i] = tail[*pos - 1];
        }
    }
    cout << length + 1 << "\n";

    int solution_length = length + 1;
    while (before[last_position] != -1) {
        solution[length--] = v[last_position];
        last_position = before[last_position];
    }
    solution[0] = v[last_position];

    for (int i = 0; i < solution_length; i++) {
        cout << solution[i] << " ";
    }
    cout << "\n";

    return 0;
}