Cod sursa(job #2286614)

Utilizator LittleWhoFeraru Mihail LittleWho Data 20 noiembrie 2018 16:14:16
Problema Subsir crescator maximal Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

const int max_n = 100000;
int n;
vector<int> v(max_n);
vector<int> best(max_n);
vector<int> before(max_n);
vector<int> solution(max_n);

int find_best_j(int i) {
    int best_j = -1;
    int sol_j = -1;

    for (int j = 1; j < i; j++) {
        if (v[j] < v[i] && best[j] > best_j) {
            best_j = best[j];
            sol_j = j;
        }
    }

    return sol_j;
}

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

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }

    int best_seq = 1;
    int seq_pos = 0;

    best[0] = 1;
    before[0] = -1;
    for (int i = 1; i < n; i++) {
        int j = find_best_j(i);
        
        if (j == -1) {
            best[i] = 1;
            before[i] = -1;
        } else {
            best[i] = 1 + best[j];
            before[i] = j;
        }

        if (best[i] > best_seq) {
            best_seq = best[i];
            seq_pos = i;
        }
    }

    cout << best_seq << "\n";

    int counter = best_seq - 1;
    int current = seq_pos;
    while (before[current] != -1) {
        solution[counter--] = v[current];
        current = before[current];
    }
    solution[0] = v[current];

    for (int i = 0; i < best_seq; i++) {
        cout << solution[i] << " "; 
    }
    cout << "\n";
 
    return 0;
}