Cod sursa(job #2749603)

Utilizator Tudor_StefanaStefana Tudor Tudor_Stefana Data 7 mai 2021 13:22:12
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include<bits/stdc++.h>
using namespace std;

ifstream fin ("scmax.in");
ofstream fout("scmax.out");


int v[100001], dp[100001], n, from[100001], lastpos, rasp[100001];

int main()
{
    int maxx = 0;
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> v[i];
    for(int i = 1; i <= n; i++)
    {
        dp[i] = 1;
        from[i] = 0;
        for(int j = 1; j <= i - 1; j++)
        {
            if(v[i] > v[j])
            {
                if(dp[i] < dp[j] + 1)
                {
                    dp[i] = dp[j] + 1;
                    from[i] = j;
                }
            }
        }


        if(dp[i] > maxx)
        {
            maxx = dp[i];
            lastpos = i;
        }
    }

    fout << maxx << "\n";
    int k = 1;
    int current = lastpos;
    while(current != 0)
    {
        rasp[k] = v[current];
        k++;
        current = from[current];
    }

    for(int i = k - 1; i >= 1; i--)
        fout << rasp[i] << " ";

}