Cod sursa(job #3364088)

Utilizator Alias47John Doe Alias47 Data 29 august 2026 09:46:33
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.91 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef size_t ull;
typedef vector<int> vc;
typedef vector<vector<int>> matrix;
#define ft(n) for(int i=1; i<=n; i++)
#define sp ' '
#define vx first
#define vy second
string file = "scmax";
ifstream f(file + ".in");
ofstream g(file + ".out");

const int INF = 2100000000;
int n;
vector<int> v, dp;

struct T
{
    int val, which;
};
vector<vector<T>> parents;

void test_parents()
{
    g << endl;
    for (int i : dp)
        g << i << sp;
    for (int i = 1; i <= n; i++)
    {
        for (T j : parents[i])
        {
            g << j.val << sp;
        }
        g << endl;
    }
}

void init()
{
    f >> n;
    v.resize(n + 1, 0);
    dp.resize(n + 1, INF);
    parents.resize(n + 1);
    ft(n)
        f >> v[i];
}

void scmax()
{
    int imax = 0;
    dp[0] = -1;
    for (int k = 1; k <= n; k++) //only for v
    {
        vector<int>::iterator it = lower_bound(dp.begin(), dp.end(), v[k]);
        if (it == dp.end());
        else if (*it == v[k]);
        else if (*it > v[k])
        {
            int i = it - dp.begin(); //for dp and parents
            if (dp[i] == INF) imax = i;
            dp[i] = v[k];
            //find parent
            //values for i==1 will never be used
            int val = dp[i - 1];
            int which = parents[i - 1].size() - 1;
            parents[i].push_back({ val, which });
        }
    }
    //output
    g << imax << endl;
    stack<int> stk;
    stk.push(dp[imax]);
    int which = parents[imax].size() - 1; //begin the path reconstruction
    for (int i = imax; i >= 2; i--)
    {
        stk.push(parents[i][which].val);
        which = parents[i][which].which;
    }
    while (stk.size())
    {
        g << stk.top() << sp;
        stk.pop();
    }
}

int main()
{
    init();
    scmax();
    //test_parents();
    return 0;
}