Cod sursa(job #2711516)

Utilizator danhHarangus Dan danh Data 24 februarie 2021 11:50:01
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");

const int NMAX = 1e5 + 5;

int dp[NMAX];

int v[NMAX];
int ant[NMAX];
stack<int> st;

int main()
{
    int n, i, j;
    ant[1] = -1;
    fin>>n;
    for(i=1; i<=n; i++)
    {
        fin>>v[i];
        dp[i] = 1;
        for(j=1; j<i; j++)
        {

            if(v[i] > v[j] && dp[j] + 1 > dp[i])
            {
                dp[i] = dp[j] + 1;
                ant[i] = j;
            }

        }
    }

    int maxim = 0, imax = 0;

    for(i=1; i<=n; i++)
    {
        if(dp[i] > maxim)
        {
             maxim = dp[i];
             imax = i;
        }
    }

    fout<<dp[imax]<<'\n';
    i = imax;
    while(i >= 1)
    {
        st.push(v[i]);
        i = ant[i];
    }

    while(!st.empty())
    {
        fout<<st.top()<<' ';
        st.pop();
    }

    fin.close();
    fout.close();
    return 0;

}