Cod sursa(job #3264114)

Utilizator TonyyAntonie Danoiu Tonyy Data 18 decembrie 2024 13:57:09
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, p, maxx;
const int Max = 1e5 + 1;
vector<int> a(Max), dp(Max, 1), parent(Max);
stack<int> s;

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

    fin >> n;
    for(int i = 1; i <= n; ++i)
        fin >> a[i];
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j < i; ++j)
            if(a[j] < a[i] && dp[i] < dp[j] + 1)
            {
                dp[i] = dp[j] + 1;
                parent[i] = j;
            }
        if(dp[i] > maxx)
        {
            maxx = dp[i];
            p = i;
        }
    }
    for(int i = p; i; i = parent[i])
        s.push(i);
    fout << maxx << "\n";
    while(!s.empty())
    {
        fout << a[s.top()] << " ";
        s.pop();
    }

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