Cod sursa(job #2255424)

Utilizator FlaviusFeteanFetean Flavius FlaviusFetean Data 6 octombrie 2018 22:20:03
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <map>
#include <iostream>
#define nMax 100005

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

int main()
{
    int n, i, lmax = 0, l, pos;
    int v[nMax] = {0}, prev[nMax] = {0}, _end[nMax] = {0}, aux[nMax] = {0};
    map<int, int> m; map<int, int>::iterator it;

    fin >> n; for(i = 1; i <= n; i++) fin >> v[i];

    for(i = 1; i <= n; i++){
        it = m.lower_bound(v[i]);
        if(it != m.end()){
            pos = ((*it).second);
            m.erase(it);m.insert(make_pair(v[i], pos));
        }
        else{
            pos = ++lmax;
            m.insert(make_pair(v[i], pos));
        }
        _end[pos] = i; prev[i] = _end[pos - 1];
    }

    l = 0;
    for(pos = _end[lmax]; pos != 0; pos = prev[pos]) _end[++l] = v[pos];
    fout << lmax << "\n"; for(i = lmax; i >0; i--) fout << _end[i] << " ";
    return 0;
}