Cod sursa(job #1503698)

Utilizator tudoras8tudoras8 tudoras8 Data 16 octombrie 2015 19:27:53
Problema Subsir 2 Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>

using namespace std;

const int MAXN = 100001;
int n, a[MAXN], best[MAXN], maxidx, maxval, TT[MAXN], pos[MAXN];

int main() {
#ifdef LOCAL
    freopen("input", "r", stdin);
#else
    ifstream cin("subsir2.in");
    ofstream cout("subsir2.out");
#endif // LOCAL

    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        best[i] = 1;
    }

    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j < i; ++j) {
            if (a[j] < a[i] && best[i] < best[j] + 1) {
                best[i] = best[j] + 1;
                TT[i] = j;
            }
        }
        if (maxval < best[i]) {
            maxval = best[i];
            maxidx = i;
        }
    }

    cout << maxval << '\n';
    int i = maxidx, j = maxval
    ;
    while (i != 0) {
        pos[j] = i;
        --j;
        i = TT[i];
    }
    for (int i = 1; i <= maxval; ++i) {
        cout << a[pos[i]] << ' ';
    }
    return 0;
}