Cod sursa(job #3264104)

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

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

int n, ans, p;
const int Max = 1e5 + 1;
vector<int> a(Max), dp(Max), 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)
    {
        int x = 0;
        for(int j = 1; j < i; ++j)
            if(a[j] > x && a[j] < a[i])
            {
                x = a[j];
                parent[i] = j;
            }
        dp[i] = 1 + dp[parent[i]];
        if(dp[i] > ans)
        {
            ans = dp[i];
            p = i;
        }
    }
    fout << ans << "\n";
    for(int i = p; i; i = parent[i])
        s.push(i);
    while(!s.empty())
    {
        fout << a[s.top()] << " ";
        s.pop();
    }

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