Cod sursa(job #2646858)

Utilizator nicu_ducalNicu Ducal nicu_ducal Data 2 septembrie 2020 11:15:14
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.94 kb
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define debug(x) cerr << #x << " = " << x << "\n"
#define all(x) (x).begin(),(x).end()
#define len(x) (x).length()
#define sqr(x) (x) * (x)

using ull = unsigned long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; /// Ordered Set : find_by_order, order_of_key
template <typename T>
ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template <typename A, typename B>
ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

const int INF = INT_MAX;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);

ll t, n;


int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    ifstream cin("scmax.in");
    ofstream cout("scmax.out");

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

    vector<int> dp(n + 1, 1), last(n + 1, -1);
    int maxlen = 1, posmax = n;
    dp[1] = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = i - 1; j > 0; j--) {
            if (a[i] > a[j] and 1 + dp[j] > dp[i]) {
                dp[i] = 1 + dp[j];
                last[i] = j;
            }
        }
        if (dp[i] > maxlen) {
            maxlen = dp[i];
            posmax = i;
        }
    }

    cout << maxlen << "\n";
    vector<int> ans;
    while (posmax != -1) {
        ans.push_back(a[posmax]);
        posmax = last[posmax];
    }
    for (int i = ans.size() - 1; i >= 0; i--)
        cout << ans[i] << " ";

    return 0;
}