Cod sursa(job #2711553)

Utilizator hax_m8Nicolae Antonio Cristian hax_m8 Data 24 februarie 2021 13:07:13
Problema Subsir crescator maximal Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <iostream>

using namespace std;

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

int main()
{
    int n, a[100001], l[100001], next[100001] = {0};
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> a[i];
    for(int i = n; i >= 1; i--)
    {
        l[i] = 1; next[i] = 0;
        for(int j = i + 1 ; j <= n; j++)
            if(a[i] <= a[j] && l[i] < l[j] + 1)
            {
                l[i] = l[j] + 1;
                next[i] = j;
            }
    }
    int curent = 2147483646;
    for(int i = 1; i <= n; i++)
    {
        if(next[i] < curent && next[i] != 0)
            curent = next[i];
    }
    /***
    for(int i = 1; i <= n; i++)
        cout << i << " ";
    cout << "\n";
    for(int i = 1; i <= n; i++)
        cout << a[i] << " ";
    cout << "\n";
    for(int i = 1; i <= n; i++)
        cout << l[i] << " ";
    cout << "\n";
    for(int i = 1; i <= n; i++)
        cout << next[i] << " ";
    cout << "\n";
    */
    int prim, subsir_max = -1;
    for(int i = 1; i <= n; i++)
        if(subsir_max < l[i])
            subsir_max = l[i];
    for(int i = 1; i <= n; i++)
        if(l[i] == subsir_max)
        {
            prim = a[i];
            break;
        }
    fout << subsir_max << "\n";
    fout << prim << " ";
    do
    {
        fout << a[curent] << " ";
        curent = next[curent];
    } while(curent != 0);
    return 0;
}