Cod sursa(job #2921712)

Utilizator TheRomulusIvan Remus TheRomulus Data 1 septembrie 2022 15:18:05
Problema Subsir crescator maximal Scor 65
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>

#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>

using namespace std;

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

typedef long long ll;

void Solve() {
    int n, endPos;
    fin >> n;
    vector<ll> v(n), ans;
    vector<pair<int, int>> c;
    for (int i = 0; i < n; ++i) {
        fin >> v[i];
    }

    for (int i = 0; i < n; ++i) {
        int pos = lower_bound(ans.begin(), ans.end(), v[i]) - ans.begin();
        pair<int, int> change;
        if (pos == ans.size()) {
            change.first = -1;
            ans.push_back(v[i]);
        }
        else {
            change.first = pos;
            change.second = ans[pos];
            ans[pos] = v[i];
        }
        c.push_back(change);
    }

    for (int i = c.size() - 1; i >= 0 && c[i].first > -1; --i) {
        int pos = c[i].first, oldValue = c[i].second;
        ans[pos] = oldValue;
    }

    fout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); ++i) {
        fout << ans[i] << ' ';
    }
    fout << '\n';
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    Solve();
    return 0;
}