Cod sursa(job #3377588)

Utilizator genius112Prodan Alexandra genius112 Data 23 noiembrie 2026 17:39:02
Problema Subsir crescator maximal Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <deque>
#include <bitset>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <random>
#include <stack>

using namespace std;

long long auxi, dp[100005], i, j, x, n, m, a[100005], fv[100005];
stack <int> s;

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

    cin >> n;

    for ( i = 1; i <= n; i++ ) {
        cin >> a[i];
        x = a[i];
        if ( m == 0 ) {
            dp[1] = x;
            m++;
        }
        else {
            auxi = lower_bound ( dp+1, dp+m+1, x )-dp;
            auxi--;
            if ( auxi >= 0 && dp[auxi] < x ) {
                dp[auxi+1] = x;
                fv[i] = auxi+1;
                if ( auxi+1 > m ) {
                    m++;
                }
            }
            else {
                dp[1] = x;
                fv[i] = 1;
            }
        }
    }

    cout << m << '\n';

    s.push(dp[m]);
    auxi = dp[m];
    m--;

    while ( i > 1 && m > 0 ) {
        i--;
        if ( auxi > a[i] && m == fv[i] ) {
            s.push(a[i]);
            auxi = a[i];
            m--;
        }
    }
    while ( !s.empty() ) {
        cout << s.top() << ' ';
        s.pop();
    }

    return 0;
}