Cod sursa(job #1997212)

Utilizator DenisacheDenis Ehorovici Denisache Data 3 iulie 2017 17:29:32
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    ios::sync_with_stdio(false);

    ifstream f("scmax.in");
    streambuf* bufIn = f.rdbuf();
    cin.rdbuf(bufIn);

    ofstream g("scmax.out");
    streambuf* bufOut = g.rdbuf();
    cout.rdbuf(bufOut);

    vector<int> vec;
    int n;

    cin >> n;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        vec.push_back(x);
    }

    vector<int> lis, sol;

    int maxLen = 0, len = 0;

    for (auto &val: vec) {
        auto pos = lower_bound(lis.begin(), lis.begin() + len, val);

        if (pos == lis.end()) {
            lis.push_back(val);
            pos = lis.end() - 1;
        }
        else {
            *pos = val;
        }

        int intPos = pos - lis.begin();
        len = intPos + 1;

        if (len > maxLen) {
            maxLen = len;
            sol = lis;
        }
    }

    cout << maxLen << "\n";

    for (auto &val : sol) {
        cout << val << " ";
    }

    return 0;
}