Cod sursa(job #3272413)

Utilizator theshadowcodertheshadowcoder theshadowcoder Data 29 ianuarie 2025 12:19:47
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

#define cin fin
#define cout fout

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

const int MAX = 1e5 + 7;
const int INF = 2e9 + 7;
int v[MAX];
int dp[MAX];
int p[MAX];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n, pmax = 0;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        p[i] = INF;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i];
        int st = 0, e = 0;
        while ((1 << (e + 1)) <= pmax)
        {
            e++;
        }
        while (e)
        {
            if (st + (1 << e) <= pmax && p[st + (1 << e)] < v[i])
            {
                st += (1 << e);
            }
            e--;
        }
        dp[i] = st + 1;
        p[dp[i]] = min(p[dp[i]], v[i]);
        pmax = max(pmax, dp[i]);
    }
    cout << pmax << '\n';
    vector <int> ans;
    for (int i = n; i > 0; i--)
    {
        if (dp[i] == pmax)
        {
            ans.push_back(v[i]);
            pmax--;
        }
    }
    reverse(ans.begin(), ans.end());
    for (auto x : ans)
    {
        cout << x << " ";
    }
    return 0;
}