Cod sursa(job #1785961)

Utilizator DanyBvGeorge-Daniel Gagiu DanyBv Data 22 octombrie 2016 10:27:17
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <cstdio>

using namespace std;

int a[100001], l[100001], t[100001], n;

int maxim(int i)
{
    int maxx = n - 1;
    for(int j = i; j < n; j++)
        if(l[j] > l[maxx] && a[j] > a[i])
            maxx = j;
    t[i] = maxx;
    return l[maxx];
}

int main()
{
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    l[n - 1] = 1;
    for(int i = n - 2; i >= 0; i--)
        l[i] = 1 + maxim(i);
    int maxi = 0;
    for(int i = 0; i < n; i++)
        if(l[i] > l[maxi])
            maxi = i;
    printf("%d\n", l[maxi]);
    for(int i = 0; i < n; i++)
        cerr << t[i] << " ";
    int i = maxi;
    while(i < n - 1)
    {
        printf("%d ", a[i]);
        if(t[i] == 0)
            break;
        else
            i = t[i];
    }
    printf("%d ", a[i]);
    return 0;
}