Cod sursa(job #2291812)

Utilizator XibronSomai Norbert-Attila Xibron Data 28 noiembrie 2018 17:43:02
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <climits>

using namespace std;

int main()
{
    int *t, n, *temp, MAX, maxh;
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);

    cin>>n;
    t = (int *)malloc((n+1)*sizeof(int));
    for(int i = 1; i<=n; i++)
        cin>>t[i];
    t[0] = 0;
    temp = (int *)calloc(n+1,sizeof(int));
    temp[n] = 1;

    for(int i = n-1; i>=0; i--)
    {
        MAX = INT_MIN;
        for(int j = i; j<=n; j++)
        {
            if(t[i]<t[j])
                if(temp[j]>MAX)
                    MAX = temp[j];
        }
        if(MAX == INT_MIN)
            temp[i] = 1;
        else
            temp[i] = MAX + 1;
    }
    cout<<temp[0]-1<<endl;
    int a = temp[0]-1;
    for(int i = 1; i<=n; i++)
    {
        if(temp[i] == a)
        {
            cout<<t[i]<<' ';
            a--;
        }
    }

    free(temp);
    free(t);
    return 0;
}