Cod sursa(job #2391528)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 28 martie 2019 22:33:38
Problema Subsir crescator maximal Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
#define NM 100010
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
void read();
int n, a[NM], pr[NM];
vector<int> v;
void afisare(int x)
{
    if(pr[x] == 0)
    {
        fout << a[x] << ' ';
        return ;
    }
    for(int i=x-1; i>=1; i--)
        if(pr[i] == pr[x]-1)
        {
            afisare(i);
            fout << a[x] << ' ';
            break;
        }
}
int main()
{
    read();
    v.push_back(a[1]);
    for(int i=2; i<=n; i++)
    {
        if(a[i] > v[v.size()-1])
        {
            v.push_back(a[i]);
            pr[i] = v.size()-1;
        }
        else
        {
            vector<int>::iterator it;
            it = upper_bound(v.begin(), v.end(), a[i]);
            *it = a[i];
            pr[i] = distance(v.begin(), it);
        }
    }
    fout << v.size() << '\n';
    int maxx = 0, ind;
    for(int i=1; i<=n; i++)
        if(pr[i] > maxx)
        {
            maxx = pr[i];
            ind = i;
        }
    afisare(ind);
    return 0;
}
void read()
{
    fin >> n;
    for(int i=1; i<=n; i++)
        fin >> a[i];
}