Cod sursa(job #795184)

Utilizator Sm3USmeu Rares Sm3U Data 7 octombrie 2012 18:42:18
Problema Subsir crescator maximal Scor 85
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int a[100010];
int s1[100010];
int s2[100010];
int n;
int lg;

void citire(){
    scanf ("%d", &n);
    for (int i = 0; i < n; ++ i){
        scanf ("%d", &a[i]);
    }
}

void subsir(){
    for (int i = 0; i < n; ++ i){
        int pos = upper_bound(s1, s1 + lg, a[i]) - s1;
        if (pos >= 0 && pos < lg){
            s1[pos] = a[i];
            s2[i] = pos;
        }else{
            if (s1[lg - 1] == a[i]){
                continue;
            }
            s1[lg ++] = a[i];
            s2[i] = lg - 1;
        }
    }
}

void afis(int i){
    if (i < 0 || lg < 0){
        return;
    }
    while (i){
        if (s2[i] == lg){
            lg --;
            afis (i - 1);
            printf ("%d ", a[i]);
            return;
        }
        i --;
    }
}

int main()
{
    freopen ("scmax.in", "r", stdin);
    freopen ("scmax.out", "w", stdout);

    citire();
    subsir();
    lg --;
    printf ("%d\n", lg + 1);
    afis(n - 1);

    return 0;
}