Cod sursa(job #3148337)

Utilizator not_anduAndu Scheusan not_andu Data 31 august 2023 10:53:07
Problema Subsir crescator maximal Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
/*

d[i] - scmax care se termina cu numarul i

! NORMALIZAM NUMERELE

*/

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

using namespace std;

#define INFILE "scmax.in"
#define OUTFILE "scmax.out"

const int VMAX = 1e5 + 2;

int n;
int v[VMAX];
vector<int> solution;

void solve(){

    cin >> n;

    for(int i = 1; i <= n; ++i){

        cin >> v[i];

    }

    solution.push_back(v[1]);

    for(int i = 2; i <= n; ++i){

        if(v[i] > solution.back()){

            solution.push_back(v[i]);

        }
        else{

            int low = lower_bound(solution.begin(), solution.end(), v[i]) - solution.begin();

            solution[low] = v[i];

        }

    }

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

    for(int i = 0; i < solution.size(); ++i){

        cout << solution[i] << " ";

    }
    
}

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

    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);

    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}