Cod sursa(job #2078271)

Utilizator lulian23Tiganescu Iulian lulian23 Data 29 noiembrie 2017 10:36:33
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
#define nmax 123456

using namespace std;

int v[ nmax ], poz[ nmax ], sol[ nmax ];
vector <int> vec;
int n;

int main(){

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

    ifstream cin("scmax.in");
    ofstream cout("scmax.out");

    cin >> n;

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

    vec.push_back(v[ 1 ]);

    for (int i = 2; i <= n; i++){
        auto it = lower_bound(vec.begin(), vec.end(), v[ i ]);

        if (it == vec.end()){
            vec.push_back(v[ i ]);
            poz[ i ] = vec.size() - 1;
        }

        else{
            poz[ i ] = it - vec.begin();
            vec[ poz[ i ] ] = v[ i ];
        }
    }

    cout << vec.size() << '\n';

    int nr = vec.size() - 1;

    for(int i = n; i; --i)
        if(nr == poz[ i ]){
        sol[++sol[ 0 ]] = v[ i ];
        --nr;
    }

    while (sol[ 0 ])
        cout << sol[sol[ 0 ]--] << " ";
}